Skip to content

Commit

Permalink
fix: add noindex to server island headers (#12827)
Browse files Browse the repository at this point in the history
* fix: add noindex to server island headers

* test: check if server island has noindex header

* chore: changeset

* refactor: set X-Robots-Tag: noindex in handler

* fix tests

---------

Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
sinskiy and ematipico authored Jan 2, 2025
1 parent 161df28 commit 7b5dc6f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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
4 changes: 4 additions & 0 deletions packages/astro/src/core/server-islands/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function createEndpoint(manifest: SSRManifest) {

// 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 +131,9 @@ export function createEndpoint(manifest: SSRManifest) {
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
29 changes: 29 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,18 @@ describe('Server islands', () => {
assert.equal(serverIslandEl.length, 0);
});

it('island is not indexed', async () => {
const res = await fixture.fetch('/_server-islands/Island', {
method: 'POST',
body: JSON.stringify({
componentExport: 'default',
encryptedProps: 'FC8337AF072BE5B1641501E1r8mLIhmIME1AV7UO9XmW9OLD',
slots: {},
}),
});
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 +86,23 @@ 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', {
method: 'POST',
body: JSON.stringify({
componentExport: 'default',
encryptedProps: 'FC8337AF072BE5B1641501E1r8mLIhmIME1AV7UO9XmW9OLD',
slots: {},
}),
headers: {
origin: 'http://example.com',
},
});
const response = await app.render(request);
assert.equal(response.headers.get('x-robots-tag'), 'noindex');
});
});
});

Expand Down

0 comments on commit 7b5dc6f

Please sign in to comment.