Skip to content

Commit

Permalink
refactor, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rtrembecky committed Dec 21, 2024
1 parent 1f4958b commit b6b928c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/api/apiAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 2 additions & 2 deletions src/api/apiInterceptor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import axios from 'axios'

import {addApiTrailingSlash} from '@/utils/addApiTrailingSlash'
import {addTrailingSlash} from '@/utils/trailingSlash'

type RequestInterceptor = Parameters<typeof axios.interceptors.request.use>[0]

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}` : ''}`
}
Expand Down
8 changes: 5 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/backendRewriteMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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}) => {
Expand All @@ -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())

Expand Down
11 changes: 0 additions & 11 deletions src/utils/addApiTrailingSlash.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/utils/trailingSlash.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b6b928c

Please sign in to comment.