-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
adapter-cloudflare: allow disabling generation of fallback 404.html
#9762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
93c4045
318c674
abb81ec
b9e8d35
d3038d7
483b580
31e29e0
714adc6
71f8465
f2413f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare': minor | ||
--- | ||
|
||
feat: allow disabling generation of `404.html` |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -36,6 +36,8 @@ export default { | |||||||||
|
||||||||||
## Options | ||||||||||
|
||||||||||
### Routes | ||||||||||
|
||||||||||
The `routes` option allows you to customise the [`_routes.json`](https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file) file generated by `adapter-cloudflare`. | ||||||||||
|
||||||||||
- `include` defines routes that will invoke a function, and defaults to `['/*']` | ||||||||||
|
@@ -47,6 +49,15 @@ The `routes` option allows you to customise the [`_routes.json`](https://develop | |||||||||
|
||||||||||
You can have up to 100 `include` and `exclude` rules combined. Generally you can omit the `routes` options, but if (for example) your `<prerendered>` paths exceed that limit, you may find it helpful to manually create an `exclude` list that includes `'/articles/*'` instead of the auto-generated `['/articles/foo', '/articles/bar', '/articles/baz', ...]`. | ||||||||||
|
||||||||||
### Generate 404 | ||||||||||
|
||||||||||
The `generate404` option allows you to specify whether a fallback `404.html` page will be generated. | ||||||||||
|
||||||||||
The default value of `auto` will only generate a fallback `404.html` page if there are *any* prerendered pages. | ||||||||||
|
||||||||||
You may want to set this to `false` if you have a fully dynamic app that does not prerender pages during build time. | ||||||||||
Comment on lines
+56
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think we need to say this, as |
||||||||||
|
||||||||||
|
||||||||||
## Deployment | ||||||||||
|
||||||||||
Please follow the [Get Started Guide](https://developers.cloudflare.com/pages/get-started) for Cloudflare Pages to begin. | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,15 @@ export interface AdapterOptions { | |
*/ | ||
exclude?: string[]; | ||
}; | ||
|
||
/** | ||
* Should a fallback 404.html be generated? | ||
* | ||
* 'auto' will only generate a fallback page if there are any prerendered pages | ||
* | ||
* @default auto | ||
*/ | ||
generate404?: boolean | 'auto'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be named something like |
||
} | ||
|
||
export interface RoutesJSONSpec { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,27 @@ export default function (options = {}) { | |
builder.rimraf(tmp); | ||
builder.mkdirp(tmp); | ||
|
||
// generate 404.html first which can then be overridden by prerendering, if the user defined such a page | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we still account for user-defined 404.html pages before generating it for them? Right now we would be overwriting them as it's generated after the prererendered pages. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea it might be better to move this back about |
||
await builder.generateFallback(path.join(dest, '404.html')); | ||
|
||
const dest_dir = `${dest}${builder.config.kit.paths.base}`; | ||
const written_files = builder.writeClient(dest_dir); | ||
builder.writePrerendered(dest_dir); | ||
|
||
const generate404 = options.generate404 ?? 'auto'; | ||
|
||
if ( | ||
(generate404 === 'auto' && builder.prerendered.paths.length > 0) || | ||
generate404 === true | ||
) { | ||
await builder.generateFallback(path.join(dest, '404.html')); | ||
} else { | ||
writeFileSync( | ||
path.join(dest_dir, builder.config.kit.appDir, '404.html'), | ||
builder | ||
.loadErrorPage() | ||
.replace(/%sveltekit\.status%/g, '404') | ||
.replace(/%sveltekit\.error\.message%/g, 'Not found') | ||
); | ||
} | ||
|
||
const relativePath = path.posix.relative(tmp, builder.getServerDirectory()); | ||
|
||
writeFileSync( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should rename this to
generateFallback
so it better matches with the error message users get during build.