kit icon indicating copy to clipboard operation
kit copied to clipboard

feat: use config.kit.paths.base for static assets

Open hmnd opened this issue 3 years ago • 10 comments

Prefixes appDir with config.kit.paths.base for adapter-(cloudflare, cloudflare-workers, netlify). Didn't touch node, as there's some discussion around how that should be handled differently in #3726. Would be great if @asendia and @tv42 could test the adapters for their respective issues, since I've never used those platforms with sveltekit.

I'm aware it would be better if the base path was handled outside of SvelteKit instead of creating nested folders like this, but (at least for workers sites), I can't think of a better alternative.

fixes #4442, fixes #2843

  • add manifest.prefix and builder.getAppPrefixDirectory()
  • adapter-cloudflare*: use manifest.prefix instead of manifest.appDir
  • adapter-netlify: use prefixed appDir for cache headers
  • adapter-vercel: use prefixed appDir for routes.json
  • adapter-cloudflare, adapter-cloudflare-workers, adapter-netlify: write static assets to "$dest/$base"

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
  • [x] This message body should clearly illustrate what problems it solves.
  • [ ] Ideally, include a test that fails without this PR but passes with it.

Tests

  • [x] Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

hmnd avatar Mar 24 '22 23:03 hmnd

🦋 Changeset detected

Latest commit: 069a17a3dc824b5f51a48d5687de9ebbb7e5e3d9

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

This PR includes changesets to release 7 packages
Name Type
@sveltejs/adapter-cloudflare Patch
@sveltejs/adapter-cloudflare-workers Patch
@sveltejs/adapter-netlify Patch
@sveltejs/adapter-node Patch
@sveltejs/adapter-vercel Patch
@sveltejs/kit Patch
@sveltejs/adapter-auto Patch

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

changeset-bot[bot] avatar Mar 24 '22 23:03 changeset-bot[bot]

Hi @hmnd, thank you for the quick action, I can confirm that the assets directory now includes the base path (path = admin): image

In the build/_headers file, I see double slash //, is this expected?

//admin/_app//*
  cache-control: public
  cache-control: immutable
  cache-control: max-age=31536000

asendia avatar Mar 25 '22 04:03 asendia

@asendia No problem. Wanted to fix this for the workers adapter, and then saw your issue pop up too.

Thanks for catching that! Should be fixed now.

hmnd avatar Mar 25 '22 04:03 hmnd

Thanks for this PR, perfect timing 👌 I gave it a try and the first deploy failed with...

17:21:23.048 | > Cannot find package 'devalue' imported from /opt/buildhome/repo/app/.svelte-kit/output/server/index.js
-- | --
17:21:23.049 | at new NodeError (node:internal/errors:371:5)
17:21:23.049 | at packageResolve (node:internal/modules/esm/resolve:932:9)
17:21:23.049 | at moduleResolve (node:internal/modules/esm/resolve:978:18)
17:21:23.049 | at defaultResolve (node:internal/modules/esm/resolve:1080:11)
17:21:23.049 | at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
17:21:23.049 | at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
17:21:23.049 | at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
17:21:23.049 | at link (node:internal/modules/esm/module_job:78:36)

I'm not sure whether it failed due to a misconfiguration on my part while running the forked version of SvelteKit, or the actual changes in this PR.

I got it fixed by adding devalue as a dependency to my app, and can confirm that the kit.paths.base configuration is now working on Cloudflare as expected 👍

arggh avatar Mar 25 '22 15:03 arggh

@asendia @arggh FYI in case you need this to work asap like me, I've published the fork at @icewolf/[email protected] while this is awaiting review. pnpm add -D "@sveltejs/kit@npm:@icewolf/svelte-kit@^1.0.0-next.303.bleeding"

hmnd avatar Mar 28 '22 21:03 hmnd

Hi, @hmnd! Can you add the fix to adapter-node too?

dmkret avatar Apr 04 '22 08:04 dmkret

@hmnd not sure if I'm doing something funky, but for some reason files in /static aren't being found when using kit.paths.base. I just get the SvelteKit 404 page. Imported assets work.

arggh avatar Apr 24 '22 14:04 arggh

@hmnd thanks for this and sorry for letting it sit! It looks like it will need to be rebased since https://github.com/sveltejs/kit/pull/5618 (removes writeStatic) and https://github.com/sveltejs/kit/pull/5332 (moves core/dev and code/build to vite folder) were merged

benmccann avatar Jul 20 '22 03:07 benmccann

@benmccann Thanks for looking at this! All rebased now. I don't have time right now to test whether the changes to netlify and vercel work correctly, so would be great if you or someone else could see if those require further changes.

hmnd avatar Jul 22 '22 03:07 hmnd

Deploy Preview for kit-demo ready!

Name Link
Latest commit 1a17ecb9959166e7664181eecc85f0f2d8a79be9
Latest deploy log https://app.netlify.com/sites/kit-demo/deploys/63115ea3ac06e00008b15dcb
Deploy Preview https://deploy-preview-4448--kit-demo.netlify.app/build
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

netlify[bot] avatar Sep 02 '22 00:09 netlify[bot]

adapter-vercel doesn't work yet, I think the base path needs to be appended where the static/client files are written, too (~line 240/241).

dummdidumm avatar Sep 30 '22 10:09 dummdidumm

I missed that the PR description said adapter-node was skipped on purpose and you preferred to handle that one separately. Anyway, I updated this PR to do the simple fix of just writing the files to a sub-directory with the base path for adapter-node in the same way as the others. That should be enough to get things working for 1.0

I created a new issue for the better fix of mounting the directories under a different path at runtime rather than changing the location on disk: https://github.com/sveltejs/kit/issues/7242

Right now, writeClient and writePrerendered are always called with the base path as a suffix, so I initially I thought we might be able to refactor by moving that inside those methods, but that wouldn't work if we allow changing mount points in the future or have dynamic base paths.

This doesn't touch adapter-static because I didn't have time and couldn't find that anyone filed an issue for that one besides the dynamic base paths issue

benmccann avatar Oct 12 '22 03:10 benmccann