Skip to content

Commit 262a9b1

Browse files
authored
Fix embed script caching (#3611)
1 parent 4f35882 commit 262a9b1

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

.changeset/nervous-rivers-destroy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix embed script and assets being cached for too long

packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/embed/script.js/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RouteLayoutParams } from '@/app/utils';
22
import { getAssetURL } from '@/lib/assets';
3+
import { buildVersion } from '@/lib/build';
34
import { getEmbeddableStaticContext } from '@/lib/embeddable';
45
import type { CreateGitBookOptions } from '@gitbook/embed';
56
import type { NextRequest } from 'next/server';
@@ -14,7 +15,6 @@ export async function GET(
1415
{ params }: { params: Promise<RouteLayoutParams> }
1516
) {
1617
const { context } = await getEmbeddableStaticContext(await params);
17-
const maxAge = 7 * 24 * 60 * 60;
1818
const initOptions: CreateGitBookOptions = {
1919
siteURL: context.linker.toAbsoluteURL(context.linker.toPathInSite('')),
2020
};
@@ -45,13 +45,13 @@ export async function GET(
4545
const load = function () {
4646
const style = document.createElement('link');
4747
style.rel = 'stylesheet';
48-
style.href = ${JSON.stringify(getAssetURL('embed/index.css'))};
48+
style.href = ${JSON.stringify(getAssetURL(`embed/index.css?v=${buildVersion()}`))};
4949
document.head.appendChild(style);
5050
5151
const script = d.createElement('script');
5252
script.type = 'text/javascript';
5353
script.async = true;
54-
script.src = ${JSON.stringify(getAssetURL('embed/index.js'))};
54+
script.src = ${JSON.stringify(getAssetURL(`embed/index.js?v=${buildVersion()}`))};
5555
5656
var latestScript = d.getElementsByTagName('script')[0];
5757
latestScript.parentNode.insertBefore(script, latestScript);
@@ -70,7 +70,7 @@ export async function GET(
7070
{
7171
headers: {
7272
'Content-Type': 'application/javascript',
73-
'Cache-Control': `public, max-age=${maxAge}, immutable`,
73+
'Cache-Control': 'public, max-age=86400, stale-while-revalidate=604800',
7474
},
7575
}
7676
);

packages/gitbook/src/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ function encodePathInSiteContent(rawPathname: string): {
552552
case 'sitemap-pages.xml':
553553
case 'robots.txt':
554554
case '~gitbook/embed/script.js':
555-
case '~gitbook/embed/assistant-demo':
555+
case '~gitbook/embed/demo':
556556
// LLMs.txt, sitemap, sitemap-pages and robots.txt are always static
557557
// as they only depend on the site structure / pages.
558558
return { pathname, routeType: 'static' };

0 commit comments

Comments
 (0)