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

Unflag Server Islands #11955

Merged
merged 10 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/strange-sheep-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'astro': minor
---

Unflag Server Islands

[Server Islands](https://astro.build/blog/future-of-astro-server-islands/), Astro's solution for highly cacheable pages of mixed static and dynamic content, is now unflagged and is available to all.

To use a server island, simply add `server:defer` to any Astro component. It will be rendered dynamically at runtime outside the context of the rest of the page, allowing you to add longer cache headers for the pages, or even prerender them.

If you are using Server Islands today, simply remove the `expermental.serverIslands` option from your Astro config.
matthewp marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 0 additions & 3 deletions examples/server-islands/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ export default defineConfig({
tailwind({ applyBaseStyles: false })
],
devToolbar: { enabled: false },
experimental: {
serverIslands: true,
}
});
3 changes: 0 additions & 3 deletions packages/astro/e2e/fixtures/server-islands/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ export default defineConfig({
adapter: nodejs({ mode: 'standalone' }),
integrations: [react(), mdx()],
trailingSlash: process.env.TRAILING_SLASH ?? 'always',
experimental: {
serverIslands: true,
}
});
13 changes: 8 additions & 5 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ class AstroBuilder {
};

const { internals, ssrOutputChunkNames, contentFileNames } = await viteBuild(opts);

const hasServerIslands = this.settings.serverIslandNameMap.size > 0;
// Error if there are server islands but no adapter provided.
if(hasServerIslands && !this.settings.adapter) {
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
throw new AstroError(AstroErrorData.NoAdapterInstalledServerIslands);
}

await staticBuild(opts, internals, ssrOutputChunkNames, contentFileNames);

// Write any additionally generated assets to disk.
Expand All @@ -225,11 +232,7 @@ class AstroBuilder {
routes: Object.values(allPages)
.flat()
.map((pageData) => pageData.route)
.concat(
this.settings.config.experimental.serverIslands
? [getServerIslandRouteData(this.settings.config)]
: [],
),
.concat(hasServerIslands ? getServerIslandRouteData(this.settings.config) : []),
logging: this.logger,
cacheManifest: internals.cacheManifestUsed,
});
Expand Down
5 changes: 1 addition & 4 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,10 @@ function generateSSRCode(settings: AstroSettings, adapter: AstroAdapter, middlew
`import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`,
`import { manifest as defaultManifest } from '${SSR_MANIFEST_VIRTUAL_MODULE_ID}';`,
edgeMiddleware ? `` : `import { onRequest as middleware } from '${middlewareId}';`,
settings.config.experimental.serverIslands
? `import { serverIslandMap } from '${VIRTUAL_ISLAND_MAP_ID}';`
: '',
`import { serverIslandMap } from '${VIRTUAL_ISLAND_MAP_ID}';`,
];

const contents = [
settings.config.experimental.serverIslands ? '' : `const serverIslandMap = new Map()`,
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
edgeMiddleware ? `const middleware = (_, next) => next()` : '',
`const _manifest = Object.assign(defaultManifest, {`,
` pageMap,`,
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const ASTRO_CONFIG_DEFAULTS = {
experimental: {
contentCollectionCache: false,
clientPrerender: false,
serverIslands: false,
contentIntellisense: false,
},
} satisfies AstroUserConfig & { server: { open: boolean } };
Expand Down Expand Up @@ -521,10 +520,6 @@ export const AstroConfigSchema = z.object({
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.clientPrerender),
serverIslands: z
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.serverIslands),
contentIntellisense: z
.boolean()
.optional()
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export async function createVite(
astroInternationalization({ settings }),
vitePluginActions({ fs, settings }),
vitePluginUserActions({ settings }),
settings.config.experimental.serverIslands && vitePluginServerIslands({ settings }),
vitePluginServerIslands({ settings }),
astroContainer(),
],
publicDir: fileURLToPath(settings.config.publicDir),
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,18 @@ export const NoAdapterInstalled = {
hint: 'See https://docs.astro.build/en/guides/server-side-rendering/ for more information.',
} satisfies ErrorData;
/**
* @docs
* @see
* - [Server-side Rendering](https://docs.astro.build/en/guides/server-side-rendering/)
* @description
* To use server islands, the same constraints exist as for sever-side rendering, so an adapter is needed.
*/
export const NoAdapterInstalledServerIslands = {
name: 'NoAdapterInstalledServerIslands',
title: 'Cannot use Server Islands without an adapter.',
message: `Cannot use server islands without an adapter. Please install and configure the appropriate server adapter for your final deployment.`,
hint: 'See https://docs.astro.build/en/guides/server-side-rendering/ for more information.',
} satisfies ErrorData;/**
matthewp marked this conversation as resolved.
Show resolved Hide resolved
* @docs
* @description
* No import statement was found for one of the components. If there is an import statement, make sure you are using the same identifier in both the imports and the component usage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ export default defineConfig({
integrations: [
svelte()
],
experimental: {
serverIslands: true,
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ export default defineConfig({
integrations: [
svelte()
],
experimental: {
serverIslands: true,
}
});

18 changes: 16 additions & 2 deletions packages/astro/test/server-islands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ describe('Server islands', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/server-islands/hybrid',
adapter: testAdapter(),
});
});

describe('build', () => {
before(async () => {
await fixture.build();
await fixture.build({
adapter: testAdapter()
});
});

it('Omits the island HTML from the static HTML', async () => {
Expand All @@ -83,5 +84,18 @@ describe('Server islands', () => {
assert.equal(serverIslandScript.length, 1, 'has the island script');
});
});

describe('build (no adapter)', () => {
it('Errors during the build', async () => {
try {
await fixture.build({
adapter: undefined
});
assert.equal(true, false, 'should not have succeeded');
} catch(err) {
assert.equal(err.title, 'Cannot use Server Islands without an adapter.');
}
});
});
});
});
Loading