reimertz
reimertz
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
Thanks so much - you really helped me unlock this and deliver somehting on a very short deadline. Truly appreciated ❤️
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
one question; could it be problematic exposing the cf parameters in a cookie, exposed to users?
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
wow, you really created a whole project to help me, truly appreciated!
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I am realizing this approach doesn't enforce the routing since a user could just go to /us
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
and then let the original path handle the request
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I was thinking; could this be a simple way to inject cf to all requests using the middleware?
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
Thanks @Dario for pointing me in the middleware direction. I got this to work locally, but will test it deploying it for preview.
//middleware.ts

import { getRequestContext } from '@cloudflare/next-on-pages'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
const context = getRequestContext()
const country = context.cf.country

if (country == 'US') return NextResponse.redirect(new URL('/us', request.url))
else return NextResponse.redirect(new URL('/rest', request.url))
}

// See "Matching Paths" below to learn more
export const config = {
matcher: '/',
}
//middleware.ts

import { getRequestContext } from '@cloudflare/next-on-pages'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
const context = getRequestContext()
const country = context.cf.country

if (country == 'US') return NextResponse.redirect(new URL('/us', request.url))
else return NextResponse.redirect(new URL('/rest', request.url))
}

// See "Matching Paths" below to learn more
export const config = {
matcher: '/',
}
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I think this PR had a good idea but again, not sure about the risks involved: https://github.com/cloudflare/next-on-pages/pull/658
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
yeah, I don't know all internals of your cloudflare and the risks involved with exposing cf.. so can't speak on the risks, but having cf accessable in getServerSideProps would open up for a lot of functionality for people still stuck with the Pages Router.
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
(thanks for being this responsive, truly helpful!)
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I am thinking about implementing a simple app/page.tsx that does the re-routing to pages/us.tsx or rest of world
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
but we're using pages router for everything right now so not sure
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I think I should be able to do that yes, we're using next-on-pages and deploying to cloudflare
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
and getRequestContext is only working in app router, right?
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
And sorry @Dario for the tone in my initial message - it is all good and thanks for all the hard work.
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
I am looking to be able to redirect users based on if they are in US or not, so I was hoping to use the cf.country property to render different pages depending on where the user is located.
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
and it being defined in as process.env.cf.country?
50 replies
CDCloudflare Developers
Created by juanferreras on 3/6/2024 in #next-on-pages
```
@Dario I spent quite some time trying to figure out why I could not access the cf context in getServerSideProps and eventually landed after quite some time. Is this limitation documented somewhere except from in this conversation?
50 replies