diff --git a/server/asset-manifest.js b/server/asset-manifest.js index 6df5494..82127d2 100644 --- a/server/asset-manifest.js +++ b/server/asset-manifest.js @@ -8,6 +8,15 @@ const tagHash = bytes => createHash('sha256').update(bytes).digest('hex').slice( export const injectBuildTag = (source, tag) => source.replace( /typeof __BUILD_TAG__ !== 'undefined' \? __BUILD_TAG__ : '[^']*'/, JSON.stringify(tag)); +function embedAssets(source, files, groups, buildTag, hashing) { + const pageFiles = {}; + for (const group of Object.values(groups)) for (const path of group.files) { + if (path !== '/index.html' && path !== '/sw.js') pageFiles[path] = { ...files[path], h: hashing ? files[path].h : buildTag }; + } + const json = JSON.stringify({ buildTag, groups, files: pageFiles }).replace(/)[\s\S]*?(<\/script>)/, (_, open, close) => open + json + close); +} + export function stampIndex(source, files, { hashing = true, buildTag = '__BUILD_TAG__' } = {}) { if (!hashing) return source.replace('__BUILD_TAG__', buildTag) .replace(/((?:href|src)=")((?![a-z]+:|\/)[^"?]+\.(?:css|js))(")/g, `$1$2?v=${buildTag}$3`); @@ -66,11 +75,11 @@ export function createAssetManifest(publicDir = './public', { hashing = true, bu // Derived build metadata cannot be an input to its own hash. Canonicalize // the index meta and SW injected tag, then publish hashes of the final bytes. // All original bytes (including index/SW source) remain inputs via source hashes. - let canonicalIndex = source === null ? null : stampIndex(source, files, { hashing }); + let canonicalIndex = source === null ? null : stampIndex(embedAssets(source, files, groups, '__BUILD_TAG__', hashing), files, { hashing }); const canonical = { assetSync, files: { ...files }, groups, contracts }; if (canonicalIndex !== null && hashing) canonical.files['/index.html'] = { ...files['/index.html'], h: assetHash(canonicalIndex), s: Buffer.byteLength(canonicalIndex), source: files['/index.html'].h }; const buildTag = hashing ? tagHash(JSON.stringify(canonical)) : (override || legacy.digest('hex').slice(0, 12)); - const index = source === null ? null : stampIndex(source, files, { hashing, buildTag }); + const index = source === null ? null : embedAssets(stampIndex(source, files, { hashing, buildTag }), files, groups, buildTag, hashing); const sw = swSource === null ? null : injectBuildTag(swSource, buildTag) .replace("typeof __ASSET_SYNC__ !== 'undefined' ? __ASSET_SYNC__ : true", JSON.stringify(assetSync)) .replace("importScripts('/asset-sync-core.js')", "importScripts('/asset-sync-core.js?v=" + (files['/asset-sync-core.js']?.h || '') + "')"); diff --git a/server/asset-manifest.test.js b/server/asset-manifest.test.js index c09a7a3..ca3a58e 100644 --- a/server/asset-manifest.test.js +++ b/server/asset-manifest.test.js @@ -72,3 +72,19 @@ test('single-tag hashing rollback also selects the legacy worker', () => fixture writeFileSync(join(dir,'sw.js'), "const VERSION = typeof __BUILD_TAG__ !== 'undefined' ? __BUILD_TAG__ : 'old'; const SYNC = typeof __ASSET_SYNC__ !== 'undefined' ? __ASSET_SYNC__ : true;"); expect(createAssetManifest(dir,{hashing:false}).sw).toContain('const SYNC = false'); })); + +test('index embeds build-local group hashes without recursively embedding index/SW hashes', () => fixture(dir => { + writeFileSync(join(dir, 'index.html'), ''); + const result=createAssetManifest(dir); + const map=JSON.parse(result.index.match(/id="ytp-assets">([^<]+)<\/script>/)[1]); + expect(map.buildTag).toBe(result.manifest.buildTag); + expect(map.groups).toEqual(result.manifest.groups); + expect(map.files['/app.js'].h).toBe(result.manifest.files['/app.js'].h); + expect(map.files['/index.html']).toBeUndefined(); + expect(map.files['/sw.js']).toBeUndefined(); + expect(createAssetManifest(dir).manifest).toEqual(result.manifest); + expect(result.manifest.files['/index.html'].h).toBe(hash(result.index)); + const off=createAssetManifest(dir,{hashing:false}); + const legacy=JSON.parse(off.index.match(/id="ytp-assets">([^<]+)<\/script>/)[1]); + expect(legacy.files['/app.js'].h).toBe(off.manifest.buildTag); +}));