-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Server Islands in Vercel (#11491)
* Fix Server Islands in Vercel * Add a changeset * Get server islands pattern from the segments * Move getPattern so it can be used at runtime * Fix build
- Loading branch information
Showing
15 changed files
with
170 additions
and
74 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,7 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix for Server Islands in Vercel adapter | ||
|
||
Vercel, and probably other adapters only allow pre-defined routes. This makes it so that the `astro:build:done` hook includes the `_server-islands/` route as part of the route data, which is used to configure available routes. |
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
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
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,57 @@ | ||
import type { | ||
AstroConfig, | ||
RoutePart, | ||
} from '../../../@types/astro.js'; | ||
|
||
export function getPattern( | ||
segments: RoutePart[][], | ||
base: AstroConfig['base'], | ||
addTrailingSlash: AstroConfig['trailingSlash'] | ||
) { | ||
const pathname = segments | ||
.map((segment) => { | ||
if (segment.length === 1 && segment[0].spread) { | ||
return '(?:\\/(.*?))?'; | ||
} else { | ||
return ( | ||
'\\/' + | ||
segment | ||
.map((part) => { | ||
if (part.spread) { | ||
return '(.*?)'; | ||
} else if (part.dynamic) { | ||
return '([^/]+?)'; | ||
} else { | ||
return part.content | ||
.normalize() | ||
.replace(/\?/g, '%3F') | ||
.replace(/#/g, '%23') | ||
.replace(/%5B/g, '[') | ||
.replace(/%5D/g, ']') | ||
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
} | ||
}) | ||
.join('') | ||
); | ||
} | ||
}) | ||
.join(''); | ||
|
||
const trailing = | ||
addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$'; | ||
let initial = '\\/'; | ||
if (addTrailingSlash === 'never' && base !== '/') { | ||
initial = ''; | ||
} | ||
return new RegExp(`^${pathname || initial}${trailing}`); | ||
} | ||
|
||
function getTrailingSlashPattern(addTrailingSlash: AstroConfig['trailingSlash']): string { | ||
if (addTrailingSlash === 'always') { | ||
return '\\/$'; | ||
} | ||
if (addTrailingSlash === 'never') { | ||
return '$'; | ||
} | ||
return '\\/?$'; | ||
} |
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
10 changes: 10 additions & 0 deletions
10
packages/integrations/vercel/test/fixtures/server-islands/astro.config.mjs
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,10 @@ | ||
import vercel from '@astrojs/vercel/serverless'; | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
output: "server", | ||
adapter: vercel(), | ||
experimental: { | ||
serverIslands: true, | ||
} | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/integrations/vercel/test/fixtures/server-islands/package.json
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,10 @@ | ||
{ | ||
"name": "@test/vercel-server-islands", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"@astrojs/vercel": "workspace:*", | ||
"astro": "workspace:*" | ||
} | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
packages/integrations/vercel/test/fixtures/server-islands/src/components/Island.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 @@ | ||
<h1>I'm an island</h1> |
12 changes: 12 additions & 0 deletions
12
packages/integrations/vercel/test/fixtures/server-islands/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,12 @@ | ||
--- | ||
import Island from '../components/Island.astro'; | ||
--- | ||
<html> | ||
<head> | ||
<title>One</title> | ||
</head> | ||
<body> | ||
<h1>One</h1> | ||
<Island server:defer /> | ||
</body> | ||
</html> |
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,29 @@ | ||
import assert from 'node:assert/strict'; | ||
import { before, describe, it } from 'node:test'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Server Islands', () => { | ||
/** @type {import('./test-utils.js').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/server-islands/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('server islands route is in the config', async () => { | ||
const config = JSON.parse( | ||
await fixture.readFile('../.vercel/output/config.json') | ||
); | ||
let found = null; | ||
for(let route of config.routes) { | ||
if(route.src?.includes('_server-islands')) { | ||
found = route; | ||
break; | ||
} | ||
} | ||
assert.notEqual(found, null, 'Default server islands route included'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.