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

feat: support maxAge for public assets #860

Merged
merged 3 commits into from
Jan 20, 2023
Merged

feat: support maxAge for public assets #860

merged 3 commits into from
Jan 20, 2023

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jan 20, 2023

Allow using maxAge for publicAssets: [] option to provide caching headers:

  publicAssets: [
    {
      baseURL: "dist",
      dir: "public/dist",
      maxAge: 3600,
    },
  ],

Remarks:

  • Using route rules in production we can also map caching to native providers
  • Default (and unused) 60sec is removed for public dirs allowing opt-in control of this feature
  • This feature is auto-disabled for / prefix and fallthrough paths since another handler might respond
  • This feature is only effective on production to reduce chance of misconfiguration and development mode issues
  • immutable and public conditions enabled
  • One can directly use routeRules to apply for development or any other custom beahior

Another fix is also included that allows to modify nitro.options.routeRules after init syncing with runtimeConfig object.

@gabrielcipriano
Copy link
Contributor

what is fallthrough in this context?

@pi0
Copy link
Member Author

pi0 commented Mar 31, 2023

When a request comes to a public asset dirs prefix with fallthrough enabled, and not matching, router continues to make a SSR request (useful for cases when a base URL is shared for public assets and API routes)

@gabrielcipriano
Copy link
Contributor

Cool, greate stuff!!

gabrielcipriano added a commit to gabrielcipriano/nitro that referenced this pull request Mar 31, 2023
Docs update suggestion based on this PR: nitrojs#860
@kkukelka
Copy link

@pi0 is this supposed to work for SSG as well? if yes, is there anything regarding the dir and baseUrl paths I have to be aware of?

@pi0
Copy link
Member Author

pi0 commented Jan 15, 2024

in SSG, there is no Nitro server running. Alotugh since we integrate with route rules, for the supported platforms (vercel, netlify, cf pages) we generated static rules for maxAge to be handled by their edge CDN.

Default TTL is not set for base asset dirs with / only to prevent unwanted caching issues.

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

Successfully merging this pull request may close these issues.

3 participants