Skip to content

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

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 11 commits into from
Mar 20, 2025

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Dec 23, 2024

closes #12216

This PR documents the fact that the two Cloudflare Pages specific files need to be excluded from the service worker's files or the service worker will fail to register if it tries to add them to the cache. This is because the files are not publicly available after the app is deployed and the service worker will error if it can't find them from the internet.

It would be nicer if we could use the adapter to exclude them but I don't know of an easy way to hook into the service worker build process or to modify the final output.

Alternatively, we could stop encouraging _headers and _redirects files in the /static directory as mentioned in the docs, and have the adapter read/copy them from the root of the project instead. Technically, they never should have been in the /static directory since they are not meant to be public files. This would be a breaking change but it does fix this issue in a better way.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@eltigerchino eltigerchino added documentation Improvements or additions to documentation pkg:adapter-cloudflare labels Dec 23, 2024
Copy link

changeset-bot bot commented Dec 23, 2024

🦋 Changeset detected

Latest commit: d294e4b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/adapter-netlify Major
@sveltejs/adapter-cloudflare Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-kit-13227-svelte.vercel.app/

this is an automated message

@dummdidumm
Copy link
Member

and have the adapter read/copy them from the root of the project instead

So people would no longer do

src/
etc/
static/
   _headers

they would put it in the root directly?

src/
etc/
_headers

Do you remember why we did it originally by putting it in static? I think it makes sense to not have them in static because as you say that's not where they will end up eventually.

@eltigerchino
Copy link
Member Author

eltigerchino commented Jan 17, 2025

and have the adapter read/copy them from the root of the project instead

So people would no longer do

src/
etc/
static/
   _headers

they would put it in the root directly?

src/
etc/
_headers

Yes, that's right.

Do you remember why we did it originally by putting it in static?

I don't think it was ever intentional but it was the only way to have the files copied over to the root of the build output automatically without changes in the adapter.

@eltigerchino eltigerchino changed the title docs: explain why _headers and _redirects files need to be excluded from the service worker's files fix: copy _headers and _redirects from project root instead of /static Jan 17, 2025
@eltigerchino eltigerchino added breaking change and removed documentation Improvements or additions to documentation labels Jan 17, 2025
@eltigerchino
Copy link
Member Author

I've changed it so that we copy the files from the project root now instead of relying on /static.

@svelte-docs-bot
Copy link

@eltigerchino eltigerchino merged commit da7b82e into main Mar 20, 2025
16 checks passed
@eltigerchino eltigerchino deleted the docs-cloudflare-service-worker branch March 20, 2025 09:26
@github-actions github-actions bot mentioned this pull request Mar 20, 2025
@colinhowells
Copy link

colinhowells commented Mar 20, 2025

6.0.0 results in this Pages deployment failure, at least for me (Kit is 2.20.1) – I'd be happy to open an issue if that's a better process for you:

Error: Failed to publish your Function. Got error: Uncaught Error: No such module "server/devalue".
  imported from "server/index.js"

Interestingly CF does not consider this a build failure? The build itself seems to work fine, but it falls down right after the worker is compiled and the assets published.

(My _headers has been moved to the project root, to comply with the breaking change. CF reports parsing those rules.)

@dummdidumm
Copy link
Member

Please open an issue with a reproduction

@colinhowells
Copy link

Will do! I believe my issue has more to do with 'fix: write server files to _worker.js/ directory' than this, based on what I'm seeing in the builds.

Currently trying to trigger the same problem in a fresh repo using some server routes to get the same _worker.js/server/index.js file with the unresolvable devalue import …

@mustofa-id
Copy link

I believe my issue has more to do with 'fix: write server files to _worker.js/ directory' than this

It seems like this is indeed the issue! I've tested it, and there’s no problem with this release.

@colinhowells
Copy link

colinhowells commented Mar 21, 2025

Well, I think it's got to do with the .svelte-kit/output/server/ directory (assuming your repo was complicated enough to require that; form actions might do it) now being at .svelte-kit/cloudflare/_worker.js/server/ and modules not being resolved in this new location

But that's enough from me here in this unrelated PR until I can actually reproduce in some minimal non-personal context

edit: Yep, modules can't resolve, this has been reverted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$service-worker files array includes non-asset _headers and _redirect files on cloudflare pages deployments
6 participants