@spark-web/next-utils
Version:
--- title: Next.js Utils isExperimentalPackage: true ---
87 lines (83 loc) • 3.26 kB
JavaScript
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _asyncToGenerator from '@babel/runtime/helpers/esm/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { renderStatic } from '@spark-web/ssr';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import { makeLinkComponent } from '@spark-web/link';
import NextLink from 'next/link';
function propsWithCssText(_x) {
return _propsWithCssText.apply(this, arguments);
}
function _propsWithCssText() {
_propsWithCssText = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee( /** Value returned from the getInitialProps static method on the Document class (in next/document). */
initialProps) {
var _yield$renderStatic, css, ids;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return renderStatic(initialProps.html);
case 2:
_yield$renderStatic = _context.sent;
css = _yield$renderStatic.css;
ids = _yield$renderStatic.ids;
return _context.abrupt("return", _objectSpread(_objectSpread({}, initialProps), {}, {
styles: /*#__PURE__*/jsxs(Fragment, {
children: [initialProps.styles, /*#__PURE__*/jsx("style", {
"data-emotion": "css ".concat(ids.join(' ')),
dangerouslySetInnerHTML: {
__html: css
}
})]
})
}));
case 6:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _propsWithCssText.apply(this, arguments);
}
var _excluded = ["href", "onClick", "rel"];
/**
* Resolves internal links using the
* [Next.js Link component](https://nextjs.org/docs/api-reference/next/link), which
* expects "href" to begin with a slash e.g. `href="/page"`. Uses a traditional
* anchor element for everything else e.g. external, hash, tel, mailto.
*
* For compatibility with TS + Spark the "href" property may only accept a
* string, so URL Objects must be resolved ahead of time. We recommend the [url
* package](https://www.npmjs.com/package/url) for complex cases, though most of
* the time it's simple to do this manually.
*/
var UniversalNextLink = makeLinkComponent(function (_ref, ref) {
var href = _ref.href,
_onClick = _ref.onClick,
rel = _ref.rel,
props = _objectWithoutProperties(_ref, _excluded);
var shouldUseNext = href[0] === '/';
return shouldUseNext ? /*#__PURE__*/jsx(NextLink, {
href: href,
passHref: true,
children: /*#__PURE__*/jsx("a", _objectSpread({
ref: ref
}, props))
}) : /*#__PURE__*/jsx("a", _objectSpread({
ref: ref,
href: href,
rel: rel || 'noreferrer noopener',
onClick: function onClick(event) {
if (href === '' || href === '#') {
event.preventDefault();
}
if (typeof _onClick === 'function') {
_onClick(event);
}
}
}, props));
});
export { UniversalNextLink, propsWithCssText };