amp-dev.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var _async_to_generator = require("@swc/helpers/lib/_async_to_generator.js").default;
  3. var _interop_require_default = require("@swc/helpers/lib/_interop_require_default.js").default;
  4. var _fouc = require("./fouc");
  5. var _onDemandEntriesClient = _interop_require_default(require("./on-demand-entries-client"));
  6. var _websocket = require("./error-overlay/websocket");
  7. const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent);
  8. window.__NEXT_DATA__ = data;
  9. let { assetPrefix , page } = data;
  10. assetPrefix = assetPrefix || '';
  11. let mostRecentHash = null;
  12. /* eslint-disable-next-line */ let curHash = __webpack_hash__;
  13. const hotUpdatePath = assetPrefix + (assetPrefix.endsWith('/') ? '' : '/') + '_next/static/webpack/';
  14. // Is there a newer version of this code available?
  15. function isUpdateAvailable() {
  16. // __webpack_hash__ is the hash of the current compilation.
  17. // It's a global variable injected by Webpack.
  18. /* eslint-disable-next-line */ return mostRecentHash !== __webpack_hash__;
  19. }
  20. // Webpack disallows updates in other states.
  21. function canApplyUpdates() {
  22. return module.hot.status() === 'idle';
  23. }
  24. function tryApplyUpdates() {
  25. return _tryApplyUpdates.apply(this, arguments);
  26. }
  27. function _tryApplyUpdates() {
  28. _tryApplyUpdates = // This function reads code updates on the fly and hard
  29. // reloads the page when it has changed.
  30. _async_to_generator(function*() {
  31. if (!isUpdateAvailable() || !canApplyUpdates()) {
  32. return;
  33. }
  34. try {
  35. const res = yield fetch(typeof __webpack_runtime_id__ !== 'undefined' ? `${hotUpdatePath}${curHash}.${__webpack_runtime_id__}.hot-update.json` : `${hotUpdatePath}${curHash}.hot-update.json`);
  36. const jsonData = yield res.json();
  37. const curPage = page === '/' ? 'index' : page;
  38. // webpack 5 uses an array instead
  39. const pageUpdated = (Array.isArray(jsonData.c) ? jsonData.c : Object.keys(jsonData.c)).some((mod)=>{
  40. return mod.indexOf(`pages${curPage.startsWith('/') ? curPage : `/${curPage}`}`) !== -1 || mod.indexOf(`pages${curPage.startsWith('/') ? curPage : `/${curPage}`}`.replace(/\//g, '\\')) !== -1;
  41. });
  42. if (pageUpdated) {
  43. document.location.reload(true);
  44. } else {
  45. curHash = mostRecentHash;
  46. }
  47. } catch (err) {
  48. console.error('Error occurred checking for update', err);
  49. document.location.reload(true);
  50. }
  51. });
  52. return _tryApplyUpdates.apply(this, arguments);
  53. }
  54. (0, _websocket).addMessageListener((event)=>{
  55. if (event.data === '\uD83D\uDC93') {
  56. return;
  57. }
  58. try {
  59. const message = JSON.parse(event.data);
  60. if (message.action === 'sync' || message.action === 'built') {
  61. if (!message.hash) {
  62. return;
  63. }
  64. mostRecentHash = message.hash;
  65. tryApplyUpdates();
  66. } else if (message.action === 'reloadPage') {
  67. document.location.reload(true);
  68. }
  69. } catch (ex) {
  70. console.warn('Invalid HMR message: ' + event.data + '\n' + ex);
  71. }
  72. });
  73. (0, _websocket).connectHMR({
  74. assetPrefix,
  75. path: '/_next/webpack-hmr'
  76. });
  77. (0, _fouc).displayContent();
  78. (0, _onDemandEntriesClient).default(data.page);
  79. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  80. Object.defineProperty(exports.default, '__esModule', { value: true });
  81. Object.assign(exports.default, exports);
  82. module.exports = exports.default;
  83. }
  84. //# sourceMappingURL=amp-dev.js.map