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

fix: add noindex to server island headers #12827

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/witty-lies-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue when crawlers try to index Server Islands thinking that Server Islands are pages
5 changes: 5 additions & 0 deletions packages/astro/src/core/server-islands/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { isAstroComponentFactory } from '../../runtime/server/render/astro/factory.js';
import { createSlotValueFromString } from '../../runtime/server/render/slot.js';
import type { ComponentInstance, ManifestData } from '../../types/astro.js';
import type { AstroGlobal } from '../../types/public/context.js';

Check warning on line 10 in packages/astro/src/core/server-islands/endpoint.ts

View workflow job for this annotation

GitHub Actions / Lint

lint/correctness/noUnusedImports

This import is unused.
import type { RouteData, SSRManifest } from '../../types/public/internal.js';
import { decryptString } from '../encryption.js';
import { getPattern } from '../routing/manifest/pattern.js';
Expand Down Expand Up @@ -104,6 +105,7 @@

// Get the request data from the body or search params
const data = await getRequestData(result.request);
// probably error
if (data instanceof Response) {
return data;
}
Expand All @@ -130,6 +132,9 @@
slots[prop] = createSlotValueFromString(data.slots[prop]);
}

// Prevent server islands from being indexed
result.response.headers.set('X-Robots-Tag', 'noindex')

// Wrap Astro components so we can set propagation to
// `self` which is needed to force the runtime to wait
// on the component before sending out the response headers.
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/server-islands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe('Server islands', () => {
assert.equal(serverIslandEl.length, 0);
});

it('island is not indexed', async () => {
const res = await fixture.fetch('/_server-islands/Island');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is failing because you're not providing a body. Change it to a POST and do like in the other test, here:

body: JSON.stringify({
componentExport: 'default',
encryptedProps: 'FC8337AF072BE5B1641501E1r8mLIhmIME1AV7UO9XmW9OLD',
slots: {},
}),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed tests and X-Robots-Tag: noindex is present even without my fix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how, there is not X-Robots-Tag in the codebase.

assert.equal(res.headers.get('X-Robots-Tag'), 'noindex');
});

it('island can set headers', async () => {
const res = await fixture.fetch('/_server-islands/Island', {
method: 'POST',
Expand Down Expand Up @@ -74,6 +79,13 @@ describe('Server islands', () => {
const serverIslandScript = $('script[data-island-id]');
assert.equal(serverIslandScript.length, 1, 'has the island script');
});

it('island is not indexed', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/_server-islands/Island');
const response = await app.render(request);
assert.equal(response.headers.get('X-Robots-Tag'), 'noindex');
});
});
});

Expand Down
Loading