-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Incremental Static Regeneration support for the Netlify's on-dema…
…nd builders adapter (#7975) * feat(netlify): expose builders ttl as a local * add changeset * docs(netlify): caching using on-demand builders * reword readme section * Update packages/integrations/netlify/package.json Co-authored-by: Bjorn Lu <[email protected]> * include builders-types.d.ts in the distribution * document caveat regarding query params * update changeset * mutation -> function * locals.netlify -> locals.runtime * update types and changeset * Apply suggestions from code review Co-authored-by: Elian ☕️ <[email protected]> * Apply suggestions from code review Co-authored-by: Elian ☕️ <[email protected]> --------- Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: Elian ☕️ <[email protected]>
- Loading branch information
1 parent
6a9fb2d
commit f974c95
Showing
7 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
'@astrojs/netlify': minor | ||
--- | ||
|
||
If you are using Netlify's On-demand Builders, you can now specify how long your pages should remain cached. By default, all pages will be rendered on first visit and reused on every subsequent visit until a redeploy. To set a custom revalidation time, call the `runtime.setBuildersTtl()` local in either your frontmatter or middleware. | ||
|
||
```astro | ||
--- | ||
import Layout from '../components/Layout.astro' | ||
if (import.meta.env.PROD) { | ||
// revalidates every 45 seconds | ||
Astro.locals.runtime.setBuildersTtl(45) | ||
} | ||
--- | ||
<Layout title="Astro on Netlify"> | ||
{new Date(Date.now())} | ||
</Layout> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
interface NetlifyLocals { | ||
runtime: { | ||
/** | ||
* On-demand Builders support an optional time to live (TTL) pattern that allows you to set a fixed duration of time after which a cached builder response is invalidated. This allows you to force a refresh of a builder-generated response without a new deploy. | ||
* @param ttl time to live, in seconds | ||
*/ | ||
setBuildersTtl(ttl: number): void | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/integrations/netlify/test/functions/builders.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { expect } from 'chai'; | ||
import { loadFixture, testIntegration } from './test-utils.js'; | ||
import netlifyAdapter from '../../dist/index.js'; | ||
|
||
describe('Builders', () => { | ||
/** @type {import('../../../astro/test/test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/builders/', import.meta.url).toString(), | ||
output: 'server', | ||
adapter: netlifyAdapter({ | ||
dist: new URL('./fixtures/builders/dist/', import.meta.url), | ||
builders: true | ||
}), | ||
site: `http://example.com`, | ||
integrations: [testIntegration()], | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('A route can set builders ttl', async () => { | ||
const entryURL = new URL( | ||
'./fixtures/builders/.netlify/functions-internal/entry.mjs', | ||
import.meta.url | ||
); | ||
const { handler } = await import(entryURL); | ||
const resp = await handler({ | ||
httpMethod: 'GET', | ||
headers: {}, | ||
rawUrl: 'http://example.com/', | ||
isBase64Encoded: false, | ||
}); | ||
expect(resp.ttl).to.equal(45); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/integrations/netlify/test/functions/fixtures/builders/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
Astro.locals.runtime.setBuildersTtl(45) | ||
--- | ||
<html> | ||
<head> | ||
<title>Astro on Netlify</title> | ||
</head> | ||
<body> | ||
<h1>{new Date(Date.now())}</h1> | ||
</body> | ||
</html> |