-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Allow adapters to customize headers for fetch requests #14543
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
Merged
Merged
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f957f03
feat: add adapter support for internal fetch headers (Netlify skew pr…
matthewp c2a0d56
fix: resolve TypeScript errors in adapter config implementation
matthewp f3c03f6
refactor: extract internalFetchHeaders from manifest into SSRResult
matthewp abaa04f
Address PR feedback: clean up tests and update comment
matthewp ea52496
Fix import sorting in create-vite and router files
matthewp 152298a
Fix skew protection test to check correct manifest file
matthewp a33869a
Fix virtual module to return empty headers during SSR
matthewp 4e36b0d
Merge branch 'main' into netlify-skew-protection
matthewp 454e770
review changes
matthewp 61a1910
Merge branch 'main' into netlify-skew-protection
matthewp b5d57a4
Update .changeset/netlify-skew-protection.md
matthewp ae1d9e1
fix lint
matthewp 1476102
Merge branch 'netlify-skew-protection' of github.com:withastro/astro …
matthewp b6d9627
Update packages/astro/dev-only.d.ts
matthewp 8a2efec
Add assetQueryParams support for skew protection
matthewp 2717a6d
Merge branch 'netlify-skew-protection' of github.com:withastro/astro …
matthewp 693b5de
update imports
matthewp d85846a
update based on review comments
matthewp 805cecc
switch to use URLSearchParams
matthewp f131229
remove unneeded file
matthewp 24cbfe7
align vercel impl with how the other works
matthewp 2c623ba
focus changesets a bit more
matthewp f515ff0
Update .changeset/astro-asset-query-params.md
matthewp da67fea
Update .changeset/astro-asset-query-params.md
matthewp f77ee2b
explain how to do it yourself too
matthewp 4639d13
Merge branch 'netlify-skew-protection' of github.com:withastro/astro …
matthewp 8490920
oops
matthewp aa361ef
Update packages/integrations/vercel/src/index.ts
matthewp a0424b6
Merge branch 'main' into netlify-skew-protection
matthewp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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': minor | ||
| --- | ||
|
|
||
| Adds two new adapter configuration options `assetQueryParams` and `internalFetchHeaders` to the Adapter API. | ||
|
|
||
| Official and community-built adapters can now use `client.assetQueryParams` to specify query parameters that should be appended to asset URLs (CSS, JavaScript, images, fonts, etc.). The query parameters are automatically appended to all generated asset URLs during the build process. | ||
|
|
||
| Adapters can also use `client.internalFetchHeaders` to specify headers that should be included in Astro's internal fetch calls (Actions, View Transitions, Server Islands, Prefetch). | ||
|
|
||
| This enables features like Netlify's skew protection, which requires the deploy ID to be sent with both internal requests and asset URLs to ensure client and server versions match during deployments. |
This file contains hidden or 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,15 @@ | ||
| --- | ||
| '@astrojs/netlify': minor | ||
| --- | ||
|
|
||
| Enables Netlify's skew protection feature for Astro sites deployed on Netlify. Skew protection ensures that your site's client and server versions stay synchronized during deployments, preventing issues where users might load assets from a newer deployment while the server is still running the older version. | ||
|
|
||
| When you deploy to Netlify, the deployment ID is now automatically included in both asset requests and API calls, allowing Netlify to serve the correct version to every user. These are set for built-in features (Actions, View Transitions, Server Islands, Prefetch). If you are making your own fetch requests to your site, you can include the header manually using the `DEPLOY_ID` environment variable: | ||
|
|
||
| ```js | ||
| const response = await fetch('/api/endpoint', { | ||
| headers: { | ||
| 'X-Netlify-Deploy-ID': import.meta.env.DEPLOY_ID, | ||
| }, | ||
| }); | ||
| ``` |
This file contains hidden or 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,7 @@ | ||
| --- | ||
| '@astrojs/vercel': minor | ||
| --- | ||
|
|
||
| Enables skew protection for Astro sites deployed on Vercel. Skew protection ensures that your site's client and server versions stay synchronized during deployments, preventing issues where users might load assets from a newer deployment while the server is still running the older version. | ||
|
|
||
| Skew protection is automatically enabled on Vercel deployments when the `VERCEL_SKEW_PROTECTION_ENABLED` environment variable is set to `1`. The deployment ID is automatically included in both asset requests and API calls, allowing Vercel to serve the correct version to every user. |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,19 @@ | ||
| import type { AssetsPrefix } from '../../core/app/types.js'; | ||
|
|
||
| export function getAssetsPrefix(fileExtension: string, assetsPrefix?: AssetsPrefix): string { | ||
| if (!assetsPrefix) return ''; | ||
| if (typeof assetsPrefix === 'string') return assetsPrefix; | ||
| // we assume the file extension has a leading '.' and we remove it | ||
| const dotLessFileExtension = fileExtension.slice(1); | ||
| if (assetsPrefix[dotLessFileExtension]) { | ||
| return assetsPrefix[dotLessFileExtension]; | ||
| export function getAssetsPrefix( | ||
| fileExtension: string, | ||
| assetsPrefix?: AssetsPrefix, | ||
| ): string { | ||
| let prefix = ''; | ||
| if (!assetsPrefix) { | ||
| prefix = ''; | ||
| } else if (typeof assetsPrefix === 'string') { | ||
| prefix = assetsPrefix; | ||
| } else { | ||
| // we assume the file extension has a leading '.' and we remove it | ||
| const dotLessFileExtension = fileExtension.slice(1); | ||
| prefix = assetsPrefix[dotLessFileExtension] || assetsPrefix.fallback; | ||
| } | ||
| return assetsPrefix.fallback; | ||
|
|
||
| return prefix; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| import type { Plugin as VitePlugin } from 'vite'; | ||
| import type { AstroSettings } from '../types/astro.js'; | ||
|
|
||
| const VIRTUAL_CLIENT_ID = 'virtual:astro:adapter-config/client'; | ||
| const RESOLVED_VIRTUAL_CLIENT_ID = '\0' + VIRTUAL_CLIENT_ID; | ||
|
|
||
| export function vitePluginAdapterConfig(settings: AstroSettings): VitePlugin { | ||
| return { | ||
| name: 'astro:adapter-config', | ||
| resolveId(id) { | ||
| if (id === VIRTUAL_CLIENT_ID) { | ||
| return RESOLVED_VIRTUAL_CLIENT_ID; | ||
| } | ||
| }, | ||
| load(id, options) { | ||
| if (id === RESOLVED_VIRTUAL_CLIENT_ID) { | ||
| // During SSR, return empty headers to avoid any runtime issues | ||
| if (options?.ssr) { | ||
| return { | ||
| code: `export const internalFetchHeaders = {};`, | ||
| }; | ||
| } | ||
|
|
||
| const adapter = settings.adapter; | ||
| const clientConfig = adapter?.client || {}; | ||
|
|
||
| let internalFetchHeaders = {}; | ||
| if (clientConfig.internalFetchHeaders) { | ||
| internalFetchHeaders = | ||
| typeof clientConfig.internalFetchHeaders === 'function' | ||
| ? clientConfig.internalFetchHeaders() | ||
| : clientConfig.internalFetchHeaders; | ||
| } | ||
|
|
||
| return { | ||
| code: `export const internalFetchHeaders = ${JSON.stringify(internalFetchHeaders)};`, | ||
| }; | ||
| } | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.