Compare commits

...

6 Commits

Author SHA1 Message Date
Samy Pessé 1424f431a9 Merge branch 'fix-embeddable-cors' of https://github.com/GitbookIO/gitbook into fix-embeddable-cors 2025-12-05 09:50:54 +01:00
Samy Pessé c2401e9a78 Remove options test 2025-12-05 09:50:53 +01:00
Samy Pessé 57625cd365 Apply suggestions from code review
Co-authored-by: conico974 <nicodorseuil@yahoo.fr>
2025-12-05 09:49:48 +01:00
Samy Pessé 0f330d5e00 Add test 2025-12-05 09:43:15 +01:00
Samy Pessé 91b9c7955b Changeset 2025-12-05 09:33:13 +01:00
Samy Pessé fc2c708ba8 Serve CORS headers on the embed/script.js route 2025-12-05 09:32:04 +01:00
3 changed files with 38 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Fix CORS error when using embed script.js directly
@@ -7,6 +7,14 @@ import type { NextRequest } from 'next/server';
export const dynamic = 'force-static';
const EMBEDDABLE_RESPONSE_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': '*',
'Cross-Origin-Resource-Policy': 'cross-origin',
'Cache-Control': 'public, s-maxage=86400, stale-while-revalidate=604800',
};
/**
* This route is used to serve the assistant.js script.
*/
@@ -69,8 +77,8 @@ export async function GET(
`,
{
headers: {
...EMBEDDABLE_RESPONSE_HEADERS,
'Content-Type': 'application/javascript',
'Cache-Control': 'public, max-age=86400, stale-while-revalidate=604800',
},
}
);
+24
View File
@@ -0,0 +1,24 @@
import { describe, expect, it } from 'bun:test';
import { getContentTestURL } from './utils';
const EMBED_SCRIPT_URL = getContentTestURL(
'https://gitbook.gitbook.io/test-gitbook-open/~gitbook/embed/script.js'
);
describe('embed script', () => {
it('serves the embeddable script with permissive headers', async () => {
const response = await fetch(EMBED_SCRIPT_URL, {
headers: {
Origin: 'https://example.com',
},
});
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toContain('application/javascript');
expect(response.headers.get('access-control-allow-origin')).toBe('*');
expect(response.headers.get('cross-origin-resource-policy')).toBe('cross-origin');
const body = await response.text();
expect(body).toContain('w.GitBook');
});
});