Skip to content
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

Fix prefetch rewrites #560

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ BE_PORT=
## developovanie proti BE na strom.sk
# BE_PROTOCOL=https
# BE_HOSTNAME=strom.sk
# BE_PORT=
# BE_PORT=

### BE headers
BE_FORWARDED_HOST=test.strom.sk
BE_FORWARDED_PROTO=https
3 changes: 3 additions & 0 deletions deployment/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ FROM node:20
ARG BE_PROTOCOL
ARG BE_HOSTNAME
ARG BE_PORT
ARG BE_FORWARDED_HOST
ARG BE_FORWARDED_PROTO
ARG DEBUG

WORKDIR /app

Expand Down
6 changes: 6 additions & 0 deletions deployment/compose-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ services:
- BE_PROTOCOL=http
- BE_HOSTNAME=localhost
- BE_PORT=8920
- BE_FORWARDED_HOST=test.strom.sk
- BE_FORWARDED_PROTO=https
- DEBUG=true

image: webstrom-test-frontend

Expand All @@ -17,6 +20,9 @@ services:
- BE_PROTOCOL=http
- BE_HOSTNAME=localhost
- BE_PORT=8920
- BE_FORWARDED_HOST=test.strom.sk
- BE_FORWARDED_PROTO=https
- DEBUG=true

network_mode: host

Expand Down
3 changes: 3 additions & 0 deletions deployment/local-mac/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ FROM node:20
ARG BE_PROTOCOL
ARG BE_HOSTNAME
ARG BE_PORT
ARG BE_FORWARDED_HOST
ARG BE_FORWARDED_PROTO
ARG DEBUG

WORKDIR /app

Expand Down
6 changes: 6 additions & 0 deletions deployment/local-mac/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ services:
- BE_PROTOCOL=http
- BE_HOSTNAME=host.docker.internal
- BE_PORT=8000
- BE_FORWARDED_HOST=test.strom.sk
- BE_FORWARDED_PROTO=https
- DEBUG=true

image: webstrom-test-frontend

Expand All @@ -15,6 +18,9 @@ services:
- BE_PROTOCOL=http
- BE_HOSTNAME=host.docker.internal
- BE_PORT=8000
- BE_FORWARDED_HOST=test.strom.sk
- BE_FORWARDED_PROTO=https
- DEBUG=true

ports:
- '3000:3000'
Expand Down
9 changes: 4 additions & 5 deletions src/api/apiAxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ export const newApiAxios = () => {
instance.interceptors.request.use((config) => {
const {method, url, baseURL} = config

debugServer('[SERVER API]', method?.toUpperCase(), url && baseURL ? new URL(url, baseURL).href : url)
debugServer('[SERVER API]', method?.toUpperCase(), baseURL, 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'
// server-side requesty z deployed FE na deployed BE potrebuju tieto hlavicky (podla settings_test.py)
config.headers['X-Forwarded-Host'] = process.env.BE_FORWARDED_HOST
config.headers['X-Forwarded-Proto'] = process.env.BE_FORWARDED_PROTO

return config
})
Expand Down
13 changes: 12 additions & 1 deletion src/middleware/backendRewriteMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ export const backendRewriteMiddleware = ({req, trailingSlash}: {req: NextRequest
// eslint-disable-next-line no-console
if (process.env.DEBUG === 'true') console.log('[MIDDLEWARE]', method, href, '->', newUrl.href)

return NextResponse.rewrite(newUrl)
// server-side requesty z deployed FE na deployed BE potrebuju tieto hlavicky (podla settings_test.py)
if (process.env.BE_FORWARDED_HOST) {
req.headers.set('Host', process.env.BE_FORWARDED_HOST)
req.headers.set('X-Forwarded-Host', process.env.BE_FORWARDED_HOST)
}
if (process.env.BE_FORWARDED_PROTO) req.headers.set('X-Forwarded-Proto', process.env.BE_FORWARDED_PROTO)

return NextResponse.rewrite(newUrl, {
request: {
headers: req.headers,
},
})
}
Loading