Skip to content

Commit

Permalink
Server Islands - Handle base + trailingSlash ignore (#11712)
Browse files Browse the repository at this point in the history
* Server Islands - Handle base + trailingSlash ignore

* Add a changeset
  • Loading branch information
matthewp authored Aug 15, 2024
1 parent 4a2cb3d commit 791d809
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-bees-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix mixed use of base + trailingSlash in Server Islands
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineConfig({
output: 'hybrid',
adapter: nodejs({ mode: 'standalone' }),
integrations: [react(), mdx()],
trailingSlash: 'always',
trailingSlash: process.env.TRAILING_SLASH ?? 'always',
experimental: {
serverIslands: true,
}
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/e2e/server-islands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ test.describe('Server islands', () => {
});
});

test.describe('Development - trailingSlash: ignore', () => {
let devServer;

test.beforeAll(async ({ astro }) => {
process.env.TRAILING_SLASH = 'ignore';
devServer = await astro.startDevServer();
});

test.afterAll(async () => {
await devServer.stop();
});

test('Load content from the server', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/base/'));
let el = page.locator('#island');

await expect(el, 'element rendered').toBeVisible();
await expect(el, 'should have content').toHaveText('I am an island');
});
});
test.describe('Production', () => {
let previewServer;

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/runtime/server/render/server-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export function renderServerIsland(
const propsEncrypted = await encryptString(key, JSON.stringify(props));

const hostId = crypto.randomUUID();
const serverIslandUrl = `${result.base}_server-islands/${componentId}${result.trailingSlash === 'always' ? '/' : ''}`;
const slash = result.base.endsWith('/') ? '' : '/';
const serverIslandUrl = `${result.base}${slash}_server-islands/${componentId}${result.trailingSlash === 'always' ? '/' : ''}`;

destination.write(`<script async type="module" data-island-id="${hostId}">
let componentId = ${safeJsonStringify(componentId)};
Expand Down

0 comments on commit 791d809

Please sign in to comment.