From 2ea142b9b2ab489e948e5caaf4738ce8851f6b95 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:19:33 +0800 Subject: [PATCH 1/8] docs --- .../60-adapter-cloudflare.md | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index 9a4b2b7e717a..35f946e1b6ac 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -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 `/static` 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. @@ -125,3 +125,25 @@ You may wish to refer to [Cloudflare's documentation for deploying a SvelteKit s ### Accessing the file system You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#prerender) the routes in question. + +### Service workers + +If you have a service worker and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed by the publicly after your app has been deployed to Cloudflare Pages. + +You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker). + +```js +import adapter from '@sveltejs/adapter-cloudflare'; + +/** @type {import('@sveltejs/kit').Config} */ +const config = { + kit: { + adapter: adapter() + serviceWorker: { + files: (filename) => !/\.DS_Store|_headers|_redirects/.test(filename) + } + } +}; + +export default config; +``` From 7874b4a146b03a67d2c5cb9af47448b32294c5b7 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:19:49 +0800 Subject: [PATCH 2/8] fix --- documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index 35f946e1b6ac..3353ecee4858 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -130,7 +130,7 @@ You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#p If you have a service worker and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed by the publicly after your app has been deployed to Cloudflare Pages. -You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker). +You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. ```js import adapter from '@sveltejs/adapter-cloudflare'; From 727e35a0cab1dd18c9779267e6bf2111a0f5f301 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:20:18 +0800 Subject: [PATCH 3/8] fix again --- documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index 3353ecee4858..c1c90a76bc50 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -128,7 +128,7 @@ You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#p ### Service workers -If you have a service worker and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed by the publicly after your app has been deployed to Cloudflare Pages. +If you have a service worker and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. From 8ee056a5972ceb35e7d53a8570df3b8b552a068c Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:20:54 +0800 Subject: [PATCH 4/8] add service worker link --- documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index c1c90a76bc50..a56b7a0482de 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -128,7 +128,7 @@ You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#p ### Service workers -If you have a service worker and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. +If you have a [service worker](https://svelte.dev/docs/kit/service-workers) and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. From 0fa4e932f2d4de8223f8293d072d7f6af58200d3 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:21:21 +0800 Subject: [PATCH 5/8] grammar --- documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index a56b7a0482de..585a0be41d6d 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -128,7 +128,7 @@ You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#p ### Service workers -If you have a [service worker](https://svelte.dev/docs/kit/service-workers) and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. +If you have a [service worker](https://svelte.dev/docs/kit/service-workers) and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all of the files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. From 9327f8d63f7c0ea6636d4ef4b363bb2faefbed88 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Mon, 23 Dec 2024 16:44:23 +0800 Subject: [PATCH 6/8] try to fix docs deployment --- documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index 585a0be41d6d..21d582a89811 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -133,6 +133,8 @@ If you have a [service worker](https://svelte.dev/docs/kit/service-workers) and You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. ```js +// @errors: 2307 +/// file: svelte.config.js import adapter from '@sveltejs/adapter-cloudflare'; /** @type {import('@sveltejs/kit').Config} */ From 2d93dfb940a3df54e582eaa0269948364e111165 Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 17 Jan 2025 10:58:56 +0800 Subject: [PATCH 7/8] copy files from project root instead of /static --- .changeset/few-apricots-study.md | 5 ++++ .../60-adapter-cloudflare.md | 26 +------------------ packages/adapter-cloudflare/index.js | 22 +++++++++++++++- 3 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 .changeset/few-apricots-study.md diff --git a/.changeset/few-apricots-study.md b/.changeset/few-apricots-study.md new file mode 100644 index 000000000000..e07db166a0ba --- /dev/null +++ b/.changeset/few-apricots-study.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-cloudflare': major +--- + +fix: copy `_headers` and `_redirects` from project root instead of `/static` directory diff --git a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md index da16e4af0f45..ab18d348d2fb 100644 --- a/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md +++ b/documentation/docs/25-build-and-deploy/60-adapter-cloudflare.md @@ -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`](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 `/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. @@ -125,27 +125,3 @@ You may wish to refer to [Cloudflare's documentation for deploying a SvelteKit s ### Accessing the file system You can't use `fs` in Cloudflare Workers — you must [prerender](page-options#prerender) the routes in question. - -### Service workers - -If you have a [service worker](https://svelte.dev/docs/kit/service-workers) and you've included the `_headers` and `_redirects` files in the `/static` directory, your service worker may fail to register if it tries to add all of the files from the `/static` directory to the cache. This is because the two files cannot be accessed publicly after your app has been deployed to Cloudflare Pages. - -You'll need to exclude the `_headers` and `_redirects` files from your service worker using the [`config.kit.serviceWorker.files`](configuration#serviceWorker) option. - -```js -// @errors: 2307 -/// file: svelte.config.js -import adapter from '@sveltejs/adapter-cloudflare'; - -/** @type {import('@sveltejs/kit').Config} */ -const config = { - kit: { - adapter: adapter() - serviceWorker: { - files: (filename) => !/\.DS_Store|_headers|_redirects/.test(filename) - } - } -}; - -export default config; -``` diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index fa47122abe29..7b9813c75b4b 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -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'; @@ -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'); @@ -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' From 88c30087eb272702082bc90cc8fef44150c7de9e Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Fri, 17 Jan 2025 11:03:02 +0800 Subject: [PATCH 8/8] Update .changeset/few-apricots-study.md --- .changeset/few-apricots-study.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/few-apricots-study.md b/.changeset/few-apricots-study.md index e07db166a0ba..3f2f8ec6e6ef 100644 --- a/.changeset/few-apricots-study.md +++ b/.changeset/few-apricots-study.md @@ -2,4 +2,4 @@ '@sveltejs/adapter-cloudflare': major --- -fix: copy `_headers` and `_redirects` from project root instead of `/static` directory +fix: copy the `_headers` and `_redirects` files from the project root instead of the `/static` directory