Unverified Commit cc254cc1 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Use customized chunk loading implementation (#37163)

Follow-up PR for #37134, this PR adds back the hash in each chunk's filename. This `chunks` in the flight manifest will now be `chunkId:chunkFilename` with this PR, and `globalThis.__next_chunk_load__` then handles the chunk registration (uses chunkId) and chunk loading (uses chunkFilename). (Q: how can we port this to React?)

We can't use `[contenthash]` but only `[chunkhash]` because at the stage of generating the flight manifest, the content isn't finalized and the hash changes after that.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 780be60d
......@@ -1017,7 +1017,6 @@ export default async function getBaseWebpackConfig(
? {
// We have to use the names here instead of hashes to ensure the consistency between compilers.
moduleIds: 'named',
chunkIds: 'named',
}
: {}),
splitChunks: ((): webpack.Options.SplitChunksOptions | false => {
......@@ -1184,7 +1183,7 @@ export default async function getBaseWebpackConfig(
? `[name].js`
: `../[name].js`
: `static/chunks/${isDevFallback ? 'fallback/' : ''}[name]${
dev ? '' : viewsDir ? '' : '-[contenthash]'
dev ? '' : viewsDir ? '-[chunkhash]' : '-[contenthash]'
}.js`,
library: isClient || isEdgeServer ? '_N_E' : undefined,
libraryTarget: isClient || isEdgeServer ? 'assign' : 'commonjs2',
......
......@@ -67,6 +67,7 @@ export class FlightManifestPlugin {
createAsset(assets: any, compilation: any) {
const manifest: any = {}
const viewsDir = this.viewsDir
const dev = this.dev
compilation.chunkGroups.forEach((chunkGroup: any) => {
function recordModule(chunk: any, id: string, mod: any) {
......@@ -102,7 +103,13 @@ export class FlightManifestPlugin {
moduleExports[name] = {
id: id.replace(/^\(sc_server\)\//, ''),
name,
chunks: viewsDir ? chunk.ids : [],
chunks: viewsDir
? chunk.ids.map((chunkId: string) => {
return (
chunkId + ':' + chunk.name + (dev ? '' : '-' + chunk.hash)
)
})
: [],
}
}
})
......
......@@ -10,10 +10,21 @@ window.next = {
// eslint-disable-next-line no-undef
const getChunkScriptFilename = __webpack_require__.u
const chunkFilenameMap = {}
// eslint-disable-next-line no-undef
__webpack_require__.u = (chunkId) => {
return getChunkScriptFilename(chunkId) || `static/chunks/${chunkId}.js`
return getChunkScriptFilename(chunkId) || chunkFilenameMap[chunkId]
}
// eslint-disable-next-line no-undef
globalThis.__next_chunk_load__ = (chunk) => {
const [chunkId, chunkFileName] = chunk.split(':')
chunkFilenameMap[chunkId] = `static/chunks/${chunkFileName}.js`
// @ts-ignore
// eslint-disable-next-line no-undef
return __webpack_chunk_load__(chunkId)
}
hydrate()
......@@ -52,7 +52,7 @@ function preloadModule(moduleData) {
var entry = chunkCache.get(chunkId);
if (entry === undefined) {
var thenable = __webpack_chunk_load__(chunkId);
var thenable = globalThis.__next_chunk_load__(chunkId);
var resolve = chunkCache.set.bind(chunkCache, chunkId, null);
var reject = chunkCache.set.bind(chunkCache, chunkId);
......
......@@ -11,7 +11,7 @@
function w(a){switch(a._status){case 3:return a._value;case 1:var b=JSON.parse(a._value,a._response._fromJSON);a._status=3;return a._value=b;case 2:b=a._value;for(var c=b.chunks,d=0;d<c.length;d++){var e=m.get(c[d]);if(null!==e)throw e;}c=__webpack_require__(b.id);b="*"===b.name?c:""===b.name?c.__esModule?c.default:c:c[b.name];a._status=3;return a._value=b;case 0:throw a;default:throw a._value;}}function x(){var a=y(this,0);return w(a)}function z(a,b){return new v(3,b,a)}
function A(a){if(null!==a)for(var b=0;b<a.length;b++)(0,a[b])()}function C(a,b){if(0===a._status){var c=a._value;a._status=4;a._value=b;A(c)}}function D(a,b){a._chunks.forEach(function(a){C(a,b)})}function y(a,b){var c=a._chunks,d=c.get(b);d||(d=new v(0,null,a),c.set(b,d));return d}
function E(a,b,c){switch(c[0]){case "$":if("$"===c)return p;if("$"===c[1]||"@"===c[1])return c.substring(1);b=parseInt(c.substring(1),16);a=y(a,b);return w(a);case "@":return b=parseInt(c.substring(1),16),a=y(a,b),{$$typeof:q,_payload:a,_init:w}}return c}function F(a){D(a,Error("Connection closed."))}
function G(a,b){if(""!==b){var c=b[0],d=b.indexOf(":",1),e=parseInt(b.substring(1,d),16);d=b.substring(d+1);switch(c){case "J":b=a._chunks;(c=b.get(e))?0===c._status&&(a=c._value,c._status=1,c._value=d,A(a)):b.set(e,new v(1,d,a));break;case "M":b=a._chunks;c=b.get(e);d=JSON.parse(d,a._fromJSON);for(var n=d.chunks,g=0;g<n.length;g++){var f=n[g];if(void 0===m.get(f)){var B=__webpack_chunk_load__(f),l=m.set.bind(m,f,null),J=m.set.bind(m,f);B.then(l,J);m.set(f,B)}}c?0===c._status&&(a=c._value,c._status=
function G(a,b){if(""!==b){var c=b[0],d=b.indexOf(":",1),e=parseInt(b.substring(1,d),16);d=b.substring(d+1);switch(c){case "J":b=a._chunks;(c=b.get(e))?0===c._status&&(a=c._value,c._status=1,c._value=d,A(a)):b.set(e,new v(1,d,a));break;case "M":b=a._chunks;c=b.get(e);d=JSON.parse(d,a._fromJSON);for(var n=d.chunks,g=0;g<n.length;g++){var f=n[g];if(void 0===m.get(f)){var B=globalThis.__next_chunk_load__(f),l=m.set.bind(m,f,null),J=m.set.bind(m,f);B.then(l,J);m.set(f,B)}}c?0===c._status&&(a=c._value,c._status=
2,c._value=d,A(a)):b.set(e,new v(2,d,a));break;case "P":a._chunks.set(e,z(a,u(d).Provider));break;case "S":c=JSON.parse(d);a._chunks.set(e,z(a,Symbol.for(c)));break;case "E":b=JSON.parse(d);c=Error(b.message);c.stack=b.stack;b=a._chunks;(d=b.get(e))?C(d,c):b.set(e,new v(4,c,a));break;default:throw Error("Error parsing the data. It's probably an error code or network corruption.");}}}
function H(a){return function(b,c){return"string"===typeof c?E(a,this,c):"object"===typeof c&&null!==c?(b=c[0]===p?{$$typeof:p,type:c[1],key:c[2],ref:null,props:c[3],_owner:null}:c,b):c}}function I(){var a=new TextDecoder;a={_chunks:new Map,readRoot:x,_partialRow:"",_stringDecoder:a};a._fromJSON=H(a);return a}
function K(a,b){function c(b){var g=b.value;if(b.done)F(a);else{b=g;g=a._stringDecoder;for(var f=b.indexOf(10);-1<f;){var n=a._partialRow;var l=b.subarray(0,f);l=g.decode(l);G(a,n+l);a._partialRow="";b=b.subarray(f+1);f=b.indexOf(10)}a._partialRow+=g.decode(b,k);return e.read().then(c,d)}}function d(b){D(a,b)}var e=b.getReader();e.read().then(c,d)}exports.createFromFetch=function(a){var b=I();a.then(function(a){K(b,a.body)},function(a){D(b,a)});return b};
......
......@@ -487,7 +487,7 @@ export async function renderToHTML(
globalThis.__webpack_require__ =
ComponentMod.__next_rsc__.__webpack_require__
// @ts-ignore
globalThis.__webpack_chunk_load__ = () => Promise.resolve()
globalThis.__next_chunk_load__ = () => Promise.resolve()
Component = createServerComponentRenderer(Component, {
cachePrefix: pathname + (search ? `?${search}` : ''),
......
......@@ -168,7 +168,7 @@ function createServerComponentRenderer(
: ComponentMod.__next_rsc__.__webpack_require__
// @ts-ignore
globalThis.__webpack_chunk_load__ = () => Promise.resolve()
globalThis.__next_chunk_load__ = () => Promise.resolve()
}
const writable = transformStream.writable
......
......@@ -1274,7 +1274,6 @@ export async function copy_react_server_dom_webpack(task, opts) {
await task
.source(require.resolve('react-server-dom-webpack'))
.target('compiled/react-server-dom-webpack')
await task
.source(
join(
......@@ -1282,6 +1281,15 @@ export async function copy_react_server_dom_webpack(task, opts) {
'cjs/react-server-dom-webpack.*'
)
)
// eslint-disable-next-line require-yield
.run({ every: true }, function* (file) {
const source = file.data.toString()
// We replace the chunk loading code with our own implementaion in Next.js.
file.data = source.replace(
/__webpack_chunk_load__/g,
'globalThis.__next_chunk_load__'
)
})
.target('compiled/react-server-dom-webpack/cjs')
await task
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment