-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: generate fallback page before compressing (#8972)
* generate fallback page before compressing - fixes #8971 * remove logging * lint --------- Co-authored-by: Simon H <[email protected]>
- Loading branch information
1 parent
275bd61
commit eb94356
Showing
5 changed files
with
59 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-static': patch | ||
--- | ||
|
||
fix: generate fallback page before compressing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
chore: refactor fallback generation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,54 @@ | ||
import { readFileSync, writeFileSync } from 'node:fs'; | ||
import { dirname, join } from 'node:path'; | ||
import { readFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { pathToFileURL } from 'node:url'; | ||
import { mkdirp } from '../../utils/filesystem.js'; | ||
import { installPolyfills } from '../../exports/node/polyfills.js'; | ||
import { load_config } from '../config/index.js'; | ||
import { forked } from '../../utils/fork.js'; | ||
|
||
const [, , dest, manifest_path, env] = process.argv; | ||
export default forked(import.meta.url, generate_fallback); | ||
|
||
/** @type {import('types').ValidatedKitConfig} */ | ||
const config = (await load_config()).kit; | ||
/** | ||
* @param {{ | ||
* manifest_path: string; | ||
* env: Record<string, string> | ||
* }} opts | ||
*/ | ||
async function generate_fallback({ manifest_path, env }) { | ||
/** @type {import('types').ValidatedKitConfig} */ | ||
const config = (await load_config()).kit; | ||
|
||
installPolyfills(); | ||
installPolyfills(); | ||
|
||
const server_root = join(config.outDir, 'output'); | ||
const server_root = join(config.outDir, 'output'); | ||
|
||
/** @type {import('types').ServerInternalModule} */ | ||
const { set_building } = await import(pathToFileURL(`${server_root}/server/internal.js`).href); | ||
/** @type {import('types').ServerInternalModule} */ | ||
const { set_building } = await import(pathToFileURL(`${server_root}/server/internal.js`).href); | ||
|
||
/** @type {import('types').ServerModule} */ | ||
const { Server } = await import(pathToFileURL(`${server_root}/server/index.js`).href); | ||
/** @type {import('types').ServerModule} */ | ||
const { Server } = await import(pathToFileURL(`${server_root}/server/index.js`).href); | ||
|
||
/** @type {import('types').SSRManifest} */ | ||
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest; | ||
/** @type {import('types').SSRManifest} */ | ||
const manifest = (await import(pathToFileURL(manifest_path).href)).manifest; | ||
|
||
set_building(true); | ||
set_building(true); | ||
|
||
const server = new Server(manifest); | ||
await server.init({ env: JSON.parse(env) }); | ||
const server = new Server(manifest); | ||
await server.init({ env }); | ||
|
||
const rendered = await server.respond(new Request(config.prerender.origin + '/[fallback]'), { | ||
getClientAddress: () => { | ||
throw new Error('Cannot read clientAddress during prerendering'); | ||
}, | ||
prerendering: { | ||
fallback: true, | ||
dependencies: new Map() | ||
}, | ||
read: (file) => readFileSync(join(config.files.assets, file)) | ||
}); | ||
const response = await server.respond(new Request(config.prerender.origin + '/[fallback]'), { | ||
getClientAddress: () => { | ||
throw new Error('Cannot read clientAddress during prerendering'); | ||
}, | ||
prerendering: { | ||
fallback: true, | ||
dependencies: new Map() | ||
}, | ||
read: (file) => readFileSync(join(config.files.assets, file)) | ||
}); | ||
|
||
mkdirp(dirname(dest)); | ||
writeFileSync(dest, await rendered.text()); | ||
if (response.ok) { | ||
return await response.text(); | ||
} | ||
|
||
throw new Error(`Could not create a fallback page — failed with status ${response.status}`); | ||
} |