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

Only cache content in /_app #1416

Merged
merged 19 commits into from
Jun 16, 2021
Merged

Only cache content in /_app #1416

merged 19 commits into from
Jun 16, 2021

Conversation

bjon
Copy link
Contributor

@bjon bjon commented May 11, 2021

Without this change, everything in the static folder will get the Cache-Control of public,max-age=31536000,immutable.
This works well if you use the assets in html/svelte files (cache busting), but not so good for favicon.ico, robots.txt, etc.

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

Changesets

  • 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

@bjon
Copy link
Contributor Author

bjon commented May 11, 2021

Just close this PR, if this is the indented behavior.

@Rich-Harris
Copy link
Member

Definitely not intended, this was an oversight. I don't think we want a separate static directory though — instead we want to ensure that ${assets}/${appDir} (where appDir is provided by the main configuration and defaults to _app) is treated as immutable but other files therein are not. We shouldn't be hard-coding 3600 either; it needs to be configurable. Ideally we'd have a way to control more than just cache headers, and on an individual basis (#809), though I'm not 100% sure what that looks like.

@Kapsonfire-DE
Copy link
Contributor

could something like a .htaccess be a solution, where you can define some things caching/header rules out of the .js code?
maybe with regex rules - server could watch for file changes, compile settingsfile on change and cache it to improve performance

@bjon
Copy link
Contributor Author

bjon commented May 11, 2021

@Rich-Harris Also Sirv options would be great to configure. For example brotli and gzip, to support precompressed files

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't aware we had this issue. Thanks for fixing it

packages/adapter-node/src/server.js Outdated Show resolved Hide resolved
@bjon
Copy link
Contributor Author

bjon commented May 12, 2021

@Rich-Harris Should I closed this? Or should I revert the static folder and fix cache in assets folder instead?

@bjon
Copy link
Contributor Author

bjon commented May 12, 2021

If we should see this as a patch, until a new, more configurable PR, is available.

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm except for the directory being hardcoded

I think I actually preferred the earlier iteration where there were two separate directories, but I will defer to Rich and am fine with this version. I do wonder if this also occurs on any of the other adapters or if they are free of this issue

packages/adapter-node/src/server.js Outdated Show resolved Hide resolved
@bjon bjon changed the title Moved 'static' to separate folder, for better cache control. Only cache content in /_app May 25, 2021
@bjon
Copy link
Contributor Author

bjon commented Jun 11, 2021

There are errors when building with latest adapter-node.
See #1667

Should i add target: 'es2020' to esbuild?
Other adapters are doing that, and it works for adapter-node as well.

@changeset-bot
Copy link

changeset-bot bot commented Jun 14, 2021

🦋 Changeset detected

Latest commit: 56c5453

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-node 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

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for sticking with this one. really close now!

immutable: true,
setHeaders: (res, pathname, stats) => {
// eslint-disable-next-line no-undef
if (pathname.startsWith(`/${esbuild_app_dir}/`)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require that the user not prefix or suffix config.kit.appDir with /. I think it'd be good to add either some sanitation or validation. E.g. we could automatically strip that prefix / suffix character. Or we could assert its value as we're doing for the base path in dc56d3c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benmccann added check for leading slash + app_dir as root

Copy link
Contributor Author

@bjon bjon Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and app_dir can not be an empty string

Copy link
Contributor Author

@bjon bjon Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe these checks are unnecessary? SvelteKit doesn't work with app_dir: "/" or app_dir: "/my_app_dir" anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should probably be fixed in another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still looks like it will not work if someone specifies config.kit.appDir with a trailing slash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaa stupid me...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benmccann fixed

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.

5 participants