Skip to content
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

fix: copy _headers and _redirects from project root instead of /static #13227

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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/few-apricots-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': major
---

fix: copy `_headers` and `_redirects` from project root instead of `/static` directory
eltigerchino marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ For testing the build, you should use [wrangler](https://developers.cloudflare.c

Functions contained in the `/functions` directory at the project's root will _not_ be included in the deployment, which is compiled to a [single `_worker.js` file](https://developers.cloudflare.com/pages/platform/functions/#advanced-mode). Functions should be implemented as [server endpoints](routing#server) in your SvelteKit app.

The `_headers` and `_redirects` files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the `/static` folder.
The [`_headers`](https://developers.cloudflare.com/pages/configuration/headers/) and [`_redirects`](https://developers.cloudflare.com/pages/configuration/redirects/) files specific to Cloudflare Pages can be used for static asset responses (like images) by putting them into the project root folder.

However, they will have no effect on responses dynamically rendered by SvelteKit, which should return custom headers or redirect responses from [server endpoints](routing#server) or with the [`handle`](hooks#Server-hooks-handle) hook.

Expand Down
22 changes: 21 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, writeFileSync } from 'node:fs';
import { copyFileSync, existsSync, writeFileSync } from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import { getPlatformProxy } from 'wrangler';
Expand All @@ -14,6 +14,18 @@ export default function (options = {}) {
);
}

if (existsSync(`${builder.config.kit.files.assets}/_headers`)) {
throw new Error(
`The _headers file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
);
}

if (existsSync(`${builder.config.kit.files.assets}/_redirects`)) {
throw new Error(
`The _redirects file should be placed in the project root rather than the ${builder.config.kit.files.assets} directory`
);
}

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const dest = builder.getBuildDirectory('cloudflare');
const tmp = builder.getBuildDirectory('cloudflare-tmp');
Expand Down Expand Up @@ -50,8 +62,16 @@ export default function (options = {}) {
JSON.stringify(get_routes_json(builder, written_files, options.routes ?? {}), null, '\t')
);

if (existsSync('_headers')) {
copyFileSync('_headers', `${dest}/_headers`);
}

writeFileSync(`${dest}/_headers`, generate_headers(builder.getAppPath()), { flag: 'a' });

if (existsSync('_redirects')) {
copyFileSync('_redirects', `${dest}/_redirects`);
}

if (builder.prerendered.redirects.size > 0) {
writeFileSync(`${dest}/_redirects`, generate_redirects(builder.prerendered.redirects), {
flag: 'a'
Expand Down
Loading