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

[vercel] support on-demand Incremental Static Invalidation (ISR) #1694

Closed
1 task done
Eckhardt-D opened this issue Sep 5, 2023 · 11 comments · Fixed by #1723
Closed
1 task done

[vercel] support on-demand Incremental Static Invalidation (ISR) #1694

Eckhardt-D opened this issue Sep 5, 2023 · 11 comments · Fixed by #1723
Labels
enhancement New feature or request preset:vercel

Comments

@Eckhardt-D
Copy link
Contributor

Describe the feature

Background

I think there is a need to support on-demand revalidation. In Vercel this could leverage the bypassToken field in the outputted prerender-config.json somehow.

Naively I would think that Nitro can build some sort of a "protected route" - that uses a runtimeConfig key for auth - which when called, will ping the Vercel function with x-prerender-revalidate: <bypassToken>.

Ideally this should be able to be done on a path level instead of the entire cache? (RFC)

Use Case

You can leverage the benefits of ISR and a long expiration, but if needed - an external service (e.g. CMS webhook) can ping your app at the nitro endpoint to revalidate the content. This will be useful for content that does not change too often, but crucial updates can be reliably made fast without caching issues or having to redeploy.

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 mentioned this issue Sep 6, 2023
1 task
@pi0 pi0 changed the title Support on-demand Incremental Static Regeneration (ISR) [vercel] support on-demand Incremental Static Invalidation (ISR) Sep 6, 2023
@pi0 pi0 added enhancement New feature or request preset:vercel and removed pending triage labels Sep 6, 2023
@hcmlopes
Copy link
Contributor

hcmlopes commented Sep 6, 2023

Wanted to add a couple of resources mentioned by Daniel R. during office hours. I am willing to try and work on this but I am not sure how far I can take it.

Vercel Build Output API

https://github.com/unjs/nitro/blob/main/src/presets/vercel.ts

@hcmlopes
Copy link
Contributor

hcmlopes commented Sep 8, 2023

I am trying to understand how nitro and the vercel preset work and it seems like the way to do this would be to have a webhook that then triggers a rebuild/redeploy of the app.

Is this a correct assumption?

I found this resource that looks to be the better way to handle this:
https://vercel.com/docs/build-output-api/v3/features#on-demand-incremental-static-regeneration-isr

@Hebilicious
Copy link
Contributor

I am trying to understand how nitro and the vercel preset work and it seems like the way to do this would be to have a webhook that then triggers a rebuild/redeploy of the app.

Is this a correct assumption?

I am no vercel expert, but I think you can just add a dummy HEAD handler, then add a bypassToken inoutput/stuff.prerender.config.json, then when you make a HEAD request to the nitro entrypoint with the correct header, vercel should invalidate the cache.

@hcmlopes
Copy link
Contributor

hcmlopes commented Sep 8, 2023

Yes @Hebilicious that seems to be correct. My question then is where should this bypassToken be set?

Vercel recommends a randomized string during build time, however this would mean that you would have to update your webhook with every deploy.

@pi0 would you or anyone else on the team have a recommendation for how this token is set? Vercel Env?

@pi0
Copy link
Member

pi0 commented Sep 8, 2023

Yeah surely you can radomize it via a vercel env variable so that it stays between build and also CI/CD to generate file with proper token.

@hcmlopes
Copy link
Contributor

hcmlopes commented Sep 11, 2023

Wouldn't making the token randomized by deploy force the user to update their webhook headers every time?

I was thinking the environment variable with the token would be set and used by the vercel preset as well as the webhook and therefore a single setup in a CMS would take care of this.

@pi0
Copy link
Member

pi0 commented Sep 11, 2023

Yes, i mean you can generate a randomized one and keep it vercel env (which is same across deploys).

Although i think in regards of DX and cross platform support of cache invalidation, we might come up with something cleaner (like an encrypted password) and use it for vercel support via abstraction.

@hcmlopes
Copy link
Contributor

@pi0 Then how would we start that process? Should I continue to focus on the vercel specific invalidation and then a new issue started to expand that or do I wait for the core team to provide some more details?

@pi0
Copy link
Member

pi0 commented Sep 12, 2023

You can directly go ahead with this feature in the meantime if like to help @hcmlopes

Types should be updated here to add an option to enable vercel cache invalidation: https://github.com/unjs/nitro/blob/main/src/types/presets.ts#L9

Logic to auto generate prerender.config should go here: https://github.com/unjs/nitro/blob/main/src/presets/vercel.ts

Documentation about generating a secret and passing it to config via env, should go here: https://github.com/unjs/nitro/blob/main/docs/content/2.deploy/providers/vercel.md

@hcmlopes
Copy link
Contributor

@pi0 I think I have the necessary code and documentation ready, however, as this is a preset change I am not sure how to go about testing.

Would you be able to lend me a hand with this step?

@pi0
Copy link
Member

pi0 commented Sep 12, 2023

Feel free to draft a PR will help on next steps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request preset:vercel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants