next-dev.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. var _interop_require_default = require("@swc/helpers/lib/_interop_require_default.js").default;
  3. var _ = require("./");
  4. var _onDemandEntriesClient = _interop_require_default(require("./dev/on-demand-entries-client"));
  5. var _webpackHotMiddlewareClient = _interop_require_default(require("./dev/webpack-hot-middleware-client"));
  6. var _devBuildWatcher = _interop_require_default(require("./dev/dev-build-watcher"));
  7. var _fouc = require("./dev/fouc");
  8. var _websocket = require("./dev/error-overlay/websocket");
  9. var _querystring = require("../shared/lib/router/utils/querystring");
  10. if (!window._nextSetupHydrationWarning) {
  11. const origConsoleError = window.console.error;
  12. window.console.error = (...args)=>{
  13. const isHydrateError = args.some((arg)=>typeof arg === 'string' && arg.match(/(hydration|content does not match|did not match)/i));
  14. if (isHydrateError) {
  15. args = [
  16. ...args,
  17. `\n\nSee more info here: https://nextjs.org/docs/messages/react-hydration-error`,
  18. ];
  19. }
  20. origConsoleError.apply(window.console, args);
  21. };
  22. window._nextSetupHydrationWarning = true;
  23. }
  24. window.next = {
  25. version: _.version,
  26. // router is initialized later so it has to be live-binded
  27. get router () {
  28. return _.router;
  29. },
  30. emitter: _.emitter
  31. };
  32. const webpackHMR = (0, _webpackHotMiddlewareClient).default();
  33. (0, _).initialize({
  34. webpackHMR
  35. }).then(({ assetPrefix })=>{
  36. (0, _websocket).connectHMR({
  37. assetPrefix,
  38. path: '/_next/webpack-hmr'
  39. });
  40. return (0, _).hydrate({
  41. beforeRender: _fouc.displayContent
  42. }).then(()=>{
  43. (0, _onDemandEntriesClient).default();
  44. let buildIndicatorHandler = ()=>{};
  45. function devPagesManifestListener(event) {
  46. if (event.data.indexOf('devPagesManifest') !== -1) {
  47. fetch(`${assetPrefix}/_next/static/development/_devPagesManifest.json`).then((res)=>res.json()).then((manifest)=>{
  48. window.__DEV_PAGES_MANIFEST = manifest;
  49. }).catch((err)=>{
  50. console.log(`Failed to fetch devPagesManifest`, err);
  51. });
  52. } else if (event.data.indexOf('middlewareChanges') !== -1) {
  53. return window.location.reload();
  54. } else if (event.data.indexOf('serverOnlyChanges') !== -1) {
  55. const { pages } = JSON.parse(event.data);
  56. // Make sure to reload when the dev-overlay is showing for an
  57. // API route
  58. if (pages.includes(_.router.query.__NEXT_PAGE)) {
  59. return window.location.reload();
  60. }
  61. if (!_.router.clc && pages.includes(_.router.pathname)) {
  62. console.log('Refreshing page data due to server-side change');
  63. buildIndicatorHandler('building');
  64. const clearIndicator = ()=>buildIndicatorHandler('built');
  65. _.router.replace(_.router.pathname + '?' + String((0, _querystring).assign((0, _querystring).urlQueryToSearchParams(_.router.query), new URLSearchParams(location.search))), _.router.asPath, {
  66. scroll: false
  67. }).catch(()=>{
  68. // trigger hard reload when failing to refresh data
  69. // to show error overlay properly
  70. location.reload();
  71. }).finally(clearIndicator);
  72. }
  73. }
  74. }
  75. (0, _websocket).addMessageListener(devPagesManifestListener);
  76. if (process.env.__NEXT_BUILD_INDICATOR) {
  77. (0, _devBuildWatcher).default((handler)=>{
  78. buildIndicatorHandler = handler;
  79. }, process.env.__NEXT_BUILD_INDICATOR_POSITION);
  80. }
  81. });
  82. }).catch((err)=>{
  83. console.error('Error was not caught', err);
  84. });
  85. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  86. Object.defineProperty(exports.default, '__esModule', { value: true });
  87. Object.assign(exports.default, exports);
  88. module.exports = exports.default;
  89. }
  90. //# sourceMappingURL=next-dev.js.map