@@ -3,7 +3,7 @@ import { type SiteURLData, fetchSiteContextByURLLookup, getBaseContext } from '@
33import { getDynamicCustomizationSettings } from '@/lib/customization' ;
44import type { SiteAPIToken } from '@gitbook/api' ;
55import { jwtDecode } from 'jwt-decode' ;
6- import { forbidden } from 'next/navigation' ;
6+ import { forbidden , notFound } from 'next/navigation' ;
77import rison from 'rison' ;
88
99export type RouteParamMode = 'url-host' | 'url' ;
@@ -80,14 +80,27 @@ export async function getDynamicSiteContext(params: RouteLayoutParams) {
8080 * Get the decoded page path from the params.
8181 */
8282export function getPagePathFromParams ( params : RouteParams ) {
83- const decoded = decodeURIComponent ( params . pagePath ) ;
84- return decoded ;
83+ // If decoding the param fails, return a 404 instead of crashing
84+ try {
85+ const decoded = decodeURIComponent ( params . pagePath ) ;
86+ return decoded ;
87+ } catch ( error ) {
88+ console . error (
89+ `Returning 404 after failing to decode page path ${ params . pagePath } : ${ error } `
90+ ) ;
91+ notFound ( ) ;
92+ }
8593}
8694
8795function getSiteURLFromParams ( params : RouteLayoutParams ) {
88- const decoded = decodeURIComponent ( params . siteURL ) ;
89- const url = new URL ( `https://${ decoded } ` ) ;
90- return url ;
96+ try {
97+ const decoded = decodeURIComponent ( params . siteURL ) ;
98+ const url = new URL ( `https://${ decoded } ` ) ;
99+ return url ;
100+ } catch ( error ) {
101+ console . error ( `Returning 404 after failing to decode site URL ${ params . siteURL } : ${ error } ` ) ;
102+ notFound ( ) ;
103+ }
91104}
92105
93106function getModeFromParams ( mode : string ) : RouteParamMode {
@@ -102,6 +115,13 @@ function getModeFromParams(mode: string): RouteParamMode {
102115 * Get the decoded site data from the params.
103116 */
104117function getSiteURLDataFromParams ( params : RouteLayoutParams ) : SiteURLData {
105- const decoded = decodeURIComponent ( params . siteData ) ;
106- return rison . decode ( decoded ) ;
118+ try {
119+ const decoded = decodeURIComponent ( params . siteData ) ;
120+ return rison . decode ( decoded ) ;
121+ } catch ( error ) {
122+ console . error (
123+ `Returning 404 after failing to decode site data ${ params . siteData } : ${ error } `
124+ ) ;
125+ notFound ( ) ;
126+ }
107127}
0 commit comments