Skip to content

breaking: generate plaintext 404.html for Cloudflare Pages, instead of SPA-style fallback #11596

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

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gold-rocks-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': major
---

breaking: generate plaintext 404.html instead of SPA-style fallback page
12 changes: 12 additions & 0 deletions packages/adapter-cloudflare/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import './ambient.js';
export default function plugin(options?: AdapterOptions): Adapter;

export interface AdapterOptions {
/**
* Whether to render a plaintext 404.html page, or a rendered SPA fallback page. This page will
* only be served when a request that matches an entry in `routes.exclude` fails to match an asset.
*
* Most of the time `plaintext` is sufficient, but if you are using `routes.exclude` to manually
* exclude a set of prerendered pages without exceeding the 100 route limit, you may wish to
* use `spa` instead to avoid showing an unstyled 404 page to users.
*
* @default 'plaintext'
*/
fallback?: 'plaintext' | 'spa';

/**
* Customize the automatically-generated `_routes.json` file
* https://developers.cloudflare.com/pages/platform/functions/routing/#create-a-_routesjson-file
Expand Down
11 changes: 9 additions & 2 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ export default function (options = {}) {

builder.rimraf(dest);
builder.rimraf(tmp);

builder.mkdirp(dest);
builder.mkdirp(tmp);

// generate 404.html first which can then be overridden by prerendering, if the user defined such a page
await builder.generateFallback(path.join(dest, '404.html'));
// generate plaintext 404.html first which can then be overridden by prerendering, if the user defined such a page
const fallback = path.join(dest, '404.html');
if (options.fallback === 'spa') {
await builder.generateFallback(fallback);
} else {
writeFileSync(fallback, 'Not Found');
}

const dest_dir = `${dest}${builder.config.kit.paths.base}`;
const written_files = builder.writeClient(dest_dir);
Expand Down