```
is there any specific reason from this limitation? Can't really tell from looking in the codebase (meaning, why it wouldn't work on
getServerSideProps
or middleware
)30 Replies
that's simply because it imports
server-only
and I noticed that that never works in the Pages router
so in order to have a nicer DX with the App router (which anyways is the recommended one) we chose to "sacrifice" the Pages router there
PS: from my point of view it is quite baffling that server-only
doesn't work with stuff like getServerSideProps
... but it does not ASAIK.... please let me know if I've missed something and there's actually away to import server-only modules from there ๐hey Dario! thanks so much for the response.
yeah I missed that on my initial message โ I was making server-only resolve to null already. It does seem simply updating to 1.10.0 it does work as expected on pages router
PS: agreed and you're correct - the whole server-only thing works due to the bundler importing differently on app router, so I don't think there's an easy way to fix. Not sure there's any flags Next.js is exposing, probably not given incremental migration from pages=>app is possible
@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?
๐
sorry about that.... I am not sure if it's documented.... I'll have a look ๐ค
by the way, in
getServerSideProps
you should be able to use process.env
๐and it being defined in as
process.env.cf.country
?
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.
And sorry @Dario for the tone in my initial message - it is all good and thanks for all the hard work.no don't worry I totally get how frustrating this sort of stuff can be, but the feedback is always appreciated ๐
yes! ๐
no!
sorry......
๐ฌ
process.env
is only for env
... cf
is accessible only in getRequestContext
....and getRequestContext is only working in app router, right?
can you set up a middleware?
I think I should be able to do that yes, we're using next-on-pages and deploying to cloudflare
yeah.... I mean, only in server side code (it uses
server-only
) and Next.js doesn't seem to recognize getServerSideProps
as server-only code! ๐but we're using pages router for everything right now so not sure
I see....
I am thinking about implementing a simple app/page.tsx that does the re-routing to pages/us.tsx or rest of world
(thanks for being this responsive, truly helpful!)
no hopefully would wouldn't have to do that ๐
I think you could just use a middleware.... let me give it a quick try....
alternatively we can expose
cf
in process.env
.... I would really really prefer not to but that's still a possibility to support use cases such as yoursyeah, 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.
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
Thanks @Dario for pointing me in the middleware direction. I got this to work locally, but will test it deploying it for preview.
yeah sorry for the delayed response I was implementing a quick app for that and yeah it does look like you should be able to use the middleware no problem ๐
I was thinking; could this be a simple way to inject
cf
to all requests using the middleware?
and then let the original path handle the requestyeah... ๐ค
ok one sec and I can share something...
I am realizing this approach doesn't enforce the routing since a user could just go to
/us
you could make your middleware match all the top level routes and always perform the check/redirect no?
anyways, a sort of generic good-enough solution could be something like this: https://github.com/dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example
what do you think?
GitHub
GitHub - dario-piotrowicz/next-on-pages-pages-router-use-cf-object-...
Contribute to dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example development by creating an account on GitHub.
this utility given a cookies string returns the
cf
object set there: https://github.com/dario-piotrowicz/next-on-pages-pages-router-use-cf-object-exampleGitHub
GitHub - dario-piotrowicz/next-on-pages-pages-router-use-cf-object-...
Contribute to dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example development by creating an account on GitHub.
the middleware sets the cookie: https://github.com/dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example/blob/main/middleware.ts
GitHub
next-on-pages-pages-router-use-cf-object-example/middleware.ts at m...
Contribute to dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example development by creating an account on GitHub.
and the utility can be used anywhere across the app: https://github.com/dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example/blob/27066d48eb080309db23c34753e61f7221d87b27/pages/index.tsx#L15
GitHub
next-on-pages-pages-router-use-cf-object-example/pages/index.tsx at...
Contribute to dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example development by creating an account on GitHub.
wow, you really created a whole project to help me, truly appreciated!
one question; could it be problematic exposing the cf parameters in a cookie, exposed to users?
no I don't think so....
but yeah better safe than sorry, we can just use headers instead of cookies ๐
I've updated the example to use headers: https://github.com/dario-piotrowicz/next-on-pages-pages-router-use-cf-object-example/commit/65c768067f4a56075159e6f02c806d0bb0e3b39b ๐
yeah using a cookie was not the best idea, simply the first thing that came to my mind ๐
(PS: of course mine is just an example and the code there could be made more robust and validation could be in place, but it's just to convey the idea that you can get the
cf
object somewhat not too painfully in your Pages router app ๐ )Thanks so much - you really helped me unlock this and deliver somehting on a very short deadline. Truly appreciated โค๏ธ
no problem, I'm happy I managed to help out ๐
we can always discuss adding built-in support for this later too, but as I said I would prefer not to, and if a workaround like this is not too painful I'd likely keep things as they are ๐ค (especially since the Pages router is not the recommended one)