rsnext/packages/next/client/page-loader.js

246 lines
6.9 KiB
JavaScript
Raw Normal View History

Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
/* global document, window */
import mitt from '../next-server/lib/mitt'
2019-07-31 18:03:38 +02:00
function supportsPreload (el) {
try {
2019-07-31 18:03:38 +02:00
return el.relList.supports('preload')
} catch {
return false
}
}
2019-07-31 18:03:38 +02:00
const hasPreload = supportsPreload(document.createElement('link'))
function preloadScript (url) {
const link = document.createElement('link')
link.rel = 'preload'
link.crossOrigin = process.crossOrigin
link.href = encodeURI(url)
2019-07-31 18:03:38 +02:00
link.as = 'script'
document.head.appendChild(link)
}
export default class PageLoader {
constructor (buildId, assetPrefix) {
this.buildId = buildId
this.assetPrefix = assetPrefix
this.pageCache = {}
this.pageRegisterEvents = mitt()
this.loadingRoutes = {}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
if (process.env.__NEXT_GRANULAR_CHUNKS) {
this.promisedBuildManifest = new Promise(resolve => {
if (window.__BUILD_MANIFEST) {
resolve(window.__BUILD_MANIFEST)
} else {
window.__BUILD_MANIFEST_CB = () => {
resolve(window.__BUILD_MANIFEST)
}
}
})
}
}
// Returns a promise for the dependencies for a particular route
getDependencies (route) {
return this.promisedBuildManifest.then(
man => (man[route] && man[route].map(url => `/_next/${url}`)) || []
)
}
normalizeRoute (route) {
if (route[0] !== '/') {
throw new Error(`Route name should start with a "/", got "${route}"`)
}
route = route.replace(/\/index$/, '/')
if (route === '/') return route
return route.replace(/\/$/, '')
}
loadPage (route) {
return this.loadPageScript(route).then(v => v.page)
}
loadPageScript (route) {
route = this.normalizeRoute(route)
return new Promise((resolve, reject) => {
const fire = ({ error, page, mod }) => {
this.pageRegisterEvents.off(route, fire)
delete this.loadingRoutes[route]
if (error) {
reject(error)
} else {
resolve({ page, mod })
}
}
// If there's a cached version of the page, let's use it.
const cachedPage = this.pageCache[route]
if (cachedPage) {
const { error, page, mod } = cachedPage
error ? reject(error) : resolve({ page, mod })
return
}
// Register a listener to get the page
this.pageRegisterEvents.on(route, fire)
// If the page is loading via SSR, we need to wait for it
// rather downloading it again.
if (document.querySelector(`script[data-next-page="${route}"]`)) {
return
}
if (!this.loadingRoutes[route]) {
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
if (process.env.__NEXT_GRANULAR_CHUNKS) {
this.getDependencies(route).then(deps => {
deps.forEach(d => {
if (!document.querySelector(`script[src^="${d}"]`)) {
this.loadScript(d, route, false)
}
})
this.loadRoute(route)
this.loadingRoutes[route] = true
})
} else {
this.loadRoute(route)
this.loadingRoutes[route] = true
}
}
})
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
async loadRoute (route) {
route = this.normalizeRoute(route)
let scriptRoute = route === '/' ? '/index.js' : `${route}.js`
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
const url = `${this.assetPrefix}/_next/static/${encodeURIComponent(
this.buildId
)}/pages${scriptRoute}`
this.loadScript(url, route, true)
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
loadScript (url, route, isPage) {
const script = document.createElement('script')
if (process.env.__NEXT_MODERN_BUILD && 'noModule' in script) {
script.type = 'module'
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
// Only page bundle scripts need to have .module added to url,
// dependencies already have it added during build manifest creation
if (isPage) url = url.replace(/\.js$/, '.module.js')
}
script.crossOrigin = process.crossOrigin
script.src = encodeURI(url)
script.onerror = () => {
const error = new Error(`Error loading script ${url}`)
error.code = 'PAGE_LOAD_ERROR'
this.pageRegisterEvents.emit(route, { error })
}
document.body.appendChild(script)
}
// This method if called by the route code.
registerPage (route, regFn) {
const register = () => {
try {
const mod = regFn()
const pageData = { page: mod.default || mod, mod }
this.pageCache[route] = pageData
this.pageRegisterEvents.emit(route, pageData)
} catch (error) {
this.pageCache[route] = { error }
this.pageRegisterEvents.emit(route, { error })
}
}
if (process.env.NODE_ENV !== 'production') {
// Wait for webpack to become idle if it's not.
// More info: https://github.com/zeit/next.js/pull/1511
if (module.hot && module.hot.status() !== 'idle') {
console.log(
`Waiting for webpack to become "idle" to initialize the page: "${route}"`
)
const check = status => {
if (status === 'idle') {
module.hot.removeStatusHandler(check)
register()
}
}
module.hot.status(check)
return
}
}
register()
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
async prefetch (route, isDependency) {
route = this.normalizeRoute(route)
let scriptRoute = `${route === '/' ? '/index' : route}.js`
if (
process.env.__NEXT_MODERN_BUILD &&
'noModule' in document.createElement('script')
) {
scriptRoute = scriptRoute.replace(/\.js$/, '.module.js')
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
const url = isDependency
? route
: `${this.assetPrefix}/_next/static/${encodeURIComponent(
this.buildId
)}/pages${scriptRoute}`
// n.b. If preload is not supported, we fall back to `loadPage` which has
// its own deduping mechanism.
if (
2019-08-16 18:00:46 +02:00
document.querySelector(
`link[rel="preload"][href^="${url}"], script[data-next-page="${route}"]`
)
) {
return
}
// Inspired by quicklink, license: https://github.com/GoogleChromeLabs/quicklink/blob/master/LICENSE
2019-07-31 18:03:38 +02:00
let cn
if ((cn = navigator.connection)) {
// Don't prefetch if the user is on 2G or if Save-Data is enabled.
if ((cn.effectiveType || '').indexOf('2g') !== -1 || cn.saveData) {
return
}
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
if (process.env.__NEXT_GRANULAR_CHUNKS && !isDependency) {
;(await this.getDependencies(route)).forEach(url => {
this.prefetch(url, true)
})
}
// Feature detection is used to see if preload is supported
// If not fall back to loading script tags before the page is loaded
// https://caniuse.com/#feat=link-rel-preload
if (hasPreload) {
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
preloadScript(url)
return
}
Experimental: Granular build chunking (#7696) * Refactor SplitChunksPlugin configs and add experimental chunking strategy * Use typeDefs for SplitChunksConfig * Modify build manifest plugin to create runtime build manifest * Add support for granular chunks to page-loader * Ensure normal behavior if experimental granularChunks flag is false * Update client build manifest to remove iife & implicit global * Factor out '/_next/' prepending into getDependencies * Update packages/next/build/webpack-config.ts filepath regex Co-Authored-By: Jason Miller <developit@users.noreply.github.com> * Simplify dependency load ordering in page-loader.js * Use SHA1 hash to shorten filenames for dependency modules * Add scheduler to framework cacheGroup in webpack-config * Update page loader to not duplicate script tags with query parameters * Ensure no slashes end up in the file hashes * Add prop-types to framework chunk * Fix issue with mis-attributed events * Increase modern build size budget--possibly decrement after consulting with @janicklasralph * Use module.rawRequest for lib chunks Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Dasherize lib chunk names Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com> * Fix typescript errors, reorganize lib name logic * Dasherize rawRequest, short circuit name logic when rawRequest found * Add `scheduler` package to test regex * Fix a nit * Adjust build manifest plugin * Shorten key name * Extract createPreloadLink helper * Extract getDependencies helper * Move method * Minimize diff * Minimize diff x2 * Fix Array.from polyfill * Simplify page loader code * Remove async=false for script tags * Code golf `getDependencies` implementation * Require lib chunks be in node_modules * Update packages/next/build/webpack-config.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Replace remaining missed windows compat regex * Trim client manifest * Prevent duplicate link preload tags * Revert size test changes * Squash manifest size even further * Add comment for clarity * Code golfing 🏌️‍♂️ * Correctly select modern dependencies * Ship separate modern client manifest when module/module enabled * Update packages/next/build/webpack/plugins/build-manifest-plugin.ts Co-Authored-By: Joe Haddad <timer150@gmail.com> * Remove unneccessary filter from page-loader * Add lookbehind to file extension regex in page-loader * v9.0.3 * Update examples for Apollo with AppTree (#8180) * Update examples for Apollo with AppTree * Fix apolloClient being overwritten when rendering AppTree * Golf page-loader (#8190) * Remove lookbehind for module replacement * Wait for build manifest promise before page load or prefetch * Updating modern-only chunks inside the right entry point * Fixing ts errors * Rename variable * Revert "Wait for build manifest promise before page load or prefetch" This reverts commit c370528c6888ba7fa71162a0854534ed280224ef. * Use proper typedef for webpack chunk * Re-enable promisified client build manifest * Fix bug in getDependencies map * Insert check for granularChunks in page-loader * Increase size limit temporarily for granular chunks * Add 50ms delay to flaky test * Set env.__NEXT_GRANULAR_CHUNKS in webpack config * Reset size limit to 187 * Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding * Update test/integration/production/test/index.test.js Co-Authored-By: Joe Haddad <timer150@gmail.com> * Do not create promise if not using chunking PR
2019-08-08 19:14:33 +02:00
if (isDependency) {
// loadPage will automatically handle depencies, so no need to
// preload them manually
return
}
if (document.readyState === 'complete') {
return this.loadPage(route).catch(() => {})
} else {
return new Promise(resolve => {
window.addEventListener('load', () => {
this.loadPage(route).then(() => resolve(), () => resolve())
})
})
}
}
}