A
A
Antonov Denis2020-08-11 18:16:06
React
Antonov Denis, 2020-08-11 18:16:06

How to properly use routing in Next.js with subdomains, with proxying?

I have an application on next.js.

The page structure is like this:

pages/
  |-- index.js
  |-- about.js
  |-- admin/
     |-- index.js
     |-- stats.js


2 subdomains are used: example.com and admin.example.com

We use apache as a server with request proxying
Launched a JS application (next start) that runs on port 3000

Added settings to the Apache config:
1) If we are knocking from the main domain, then requests go to localhost:3000/*
2) If we knock from a subdomain (admin.example.com), then requests go to localhost:3000/admin/*

The essence of the problem is as follows.
When I walk through the pages of the main domain (example.com -> example.com/about), the site works like a SPA, the page is not updated, but simply drawn from JS files.
And when I walk on a subdomain, then with each click (admin.example.com -> admin.example.com/stats) the page refreshes.

From errors in JS, I realized that the matter is in routing and request proxying.
I go to admin.example.com -> admin.example.com/stats on the client. JS looks for /pages/stats.js file - it doesn't exist. The page refreshes and thanks to proxying, the /pages/admin/stats.js file is searched for on the first load and everything is displayed correctly.

How can you do a normal job in this case? Maybe somehow specify in the _app.js file - if you are on a subdomain, then look for pages in the /pages/admin/* folder? Or maybe I have not the right approach to this problem at all?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question