Skip to content

Encode customization header #3289

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

Merged
merged 2 commits into from
Jun 9, 2025
Merged
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
6 changes: 6 additions & 0 deletions .changeset/lazy-pants-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook-v2": patch
"gitbook": patch
---

encode customization header
3 changes: 2 additions & 1 deletion packages/gitbook-v2/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
const customization = siteRequestURL.searchParams.get('customization');
if (customization && validateSerializedCustomization(customization)) {
routeType = 'dynamic';
requestHeaders.set(MiddlewareHeaders.Customization, customization);
// We need to encode the customization headers, otherwise it will fail for some customization values containing non ASCII chars on vercel.
requestHeaders.set(MiddlewareHeaders.Customization, encodeURIComponent(customization));
}
const theme = siteRequestURL.searchParams.get('theme');
if (theme === CustomizationThemeMode.Dark || theme === CustomizationThemeMode.Light) {
Expand Down
5 changes: 4 additions & 1 deletion packages/gitbook/src/lib/customization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ export async function getDynamicCustomizationSettings(
): Promise<SiteCustomizationSettings> {
const headersList = await headers();
const extend = headersList.get(MiddlewareHeaders.Customization);

if (extend) {
try {
const parsedSettings = rison.decode_object<SiteCustomizationSettings>(extend);
// We need to decode it first as it is URL encoded, then decode the Rison object
const unencoded = decodeURIComponent(extend);
const parsedSettings = rison.decode_object<SiteCustomizationSettings>(unencoded);

return parsedSettings;
} catch (_error) {}
Expand Down
4 changes: 3 additions & 1 deletion packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ export async function middleware(request: NextRequest) {

const customization = url.searchParams.get('customization');
if (customization && validateSerializedCustomization(customization)) {
headers.set(MiddlewareHeaders.Customization, customization);
// We need to encode the customization to ensure it is properly encoded in vercel.
// We do it here as well so that we have a single method to decode it later.
headers.set(MiddlewareHeaders.Customization, encodeURIComponent(customization));
}

const theme = url.searchParams.get('theme');
Expand Down