diff --git a/src/api/apiAxios.ts b/src/api/apiAxios.ts index 3677e0b4..4b722458 100644 --- a/src/api/apiAxios.ts +++ b/src/api/apiAxios.ts @@ -29,6 +29,8 @@ export const newApiAxios = (base: 'server' | 'client') => { debugServer('[SERVER API]', method?.toUpperCase(), url && baseURL ? new URL(url, baseURL).href : url) + // server-side requesty z deployed FE na deployed BE potrebuju tieto hlavicky + // TODO: ked pojdeme do produkcie, asi bude treba riesit nejakym env varom config.headers['X-Forwarded-Host'] = 'test.strom.sk' config.headers['X-Forwarded-Proto'] = 'https' diff --git a/src/api/apiInterceptor.ts b/src/api/apiInterceptor.ts index 6a3b5289..1607d232 100644 --- a/src/api/apiInterceptor.ts +++ b/src/api/apiInterceptor.ts @@ -1,6 +1,6 @@ import axios from 'axios' -import {addApiTrailingSlash} from '@/utils/addApiTrailingSlash' +import {addTrailingSlash} from '@/utils/trailingSlash' type RequestInterceptor = Parameters[0] @@ -8,7 +8,7 @@ export const apiInterceptor: RequestInterceptor = (config) => { if (config.url) { const [pathname, query] = config.url.split('?') - const newPathname = addApiTrailingSlash(pathname) + const newPathname = addTrailingSlash(pathname) config.url = `${newPathname}${query ? `?${query}` : ''}` } diff --git a/src/middleware.ts b/src/middleware.ts index dd9b97ff..88e725c7 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,6 +1,7 @@ import {NextRequest, NextResponse} from 'next/server' import {backendRewriteMiddleware} from './middleware/backendRewriteMiddleware' +import {removeTrailingSlash} from './utils/trailingSlash' export function middleware(req: NextRequest) { const url = req.nextUrl @@ -16,9 +17,10 @@ export function middleware(req: NextRequest) { // odstran trailing slash - default next.js spravanie, ale vypli sme ho v next.config.ts pomocou // `skipTrailingSlashRedirect: true`, aby sme dovolili (a v axiose a middlewari vyssie aj forcli) // trailing slash pre BE Django - // pre root route `/` to nemozeme spravit (infinite redirect) ¯\_(ツ)_/¯ - if (url.pathname.endsWith('/') && url.pathname !== '/') { - const newPathname = url.pathname.slice(0, -1) + const newPathname = removeTrailingSlash(url.pathname) + + // redirect ak sa da odstranit trailing slash + if (newPathname !== url.pathname) { const newUrl = new URL(`${newPathname}${url.search}`, url) return NextResponse.redirect(newUrl, 308) diff --git a/src/middleware/backendRewriteMiddleware.ts b/src/middleware/backendRewriteMiddleware.ts index ddef3b5d..5c50b85b 100644 --- a/src/middleware/backendRewriteMiddleware.ts +++ b/src/middleware/backendRewriteMiddleware.ts @@ -1,6 +1,6 @@ import {NextRequest, NextResponse} from 'next/server' -import {addApiTrailingSlash} from '@/utils/addApiTrailingSlash' +import {addTrailingSlash} from '@/utils/trailingSlash' import {getBackendServerUrl} from '@/utils/urlBase' export const backendRewriteMiddleware = ({req, trailingSlash}: {req: NextRequest; trailingSlash: boolean}) => { @@ -9,7 +9,7 @@ export const backendRewriteMiddleware = ({req, trailingSlash}: {req: NextRequest let newPathname = pathname - if (trailingSlash) newPathname = addApiTrailingSlash(newPathname) + if (trailingSlash) newPathname = addTrailingSlash(newPathname) const newUrl = new URL(`${newPathname}${search}`, getBackendServerUrl()) diff --git a/src/utils/addApiTrailingSlash.ts b/src/utils/addApiTrailingSlash.ts deleted file mode 100644 index d9709b2a..00000000 --- a/src/utils/addApiTrailingSlash.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * BE Django server ocakava trailing slash (aj pred query params). - * - * @param pathname local URL without query params. e.g. `/api/cms/post` - * @returns local URL with trailing slash. e.g. `/api/cms/post/` - * */ -export const addApiTrailingSlash = (pathname: string) => { - if (!pathname.endsWith('/')) return `${pathname}/` - - return pathname -} diff --git a/src/utils/trailingSlash.ts b/src/utils/trailingSlash.ts new file mode 100644 index 00000000..622fd066 --- /dev/null +++ b/src/utils/trailingSlash.ts @@ -0,0 +1,20 @@ +/** + * BE Django server ocakava trailing slash (aj pred query params). + * + * @param pathname local URL without query params. e.g. `/cms/post` + * @returns local URL with trailing slash. e.g. `/cms/post/` + * */ +export const addTrailingSlash = (pathname: string) => { + if (!pathname.endsWith('/')) return `${pathname}/` + + return pathname +} + +export const removeTrailingSlash = (pathname: string) => { + // pre root route `/` nemozeme odstranit slash (infinite redirect) + if (pathname === '/') return pathname + + if (pathname.endsWith('/')) return pathname.slice(0, -1) + + return pathname +}