I’m working on a multi-tenant NextJS application (e.g., “tenant1.example.com” and “tenant2.example.com”).
When the web app first starts, it loads the “get-user-pools” module, which retrieves some Cognito user pools from a DB using the “loadPoolsFromDb” function (currently, it is just using an array instead of going to a real DB).
After a user logs in, they are redirected to the “/dashboard” page. The problem is that every time the user goes to this page, or reloads the page using the browser, the app will load the “get-user-pools” module again. This is really bad, of course, as it also results in the “loadPoolsFromDb” function running again. From the server’s console:
GET /dashboard 200 in 73ms
"get-user-pools" module loading
"loadPoolsFromDb" function started
I simplified the “dashboard” page to a simple H1 header and a simple Server Action that is triggered by a button.
Dashboard page:
import * as actions from '@/actions';
import { Button } from '@mantine/core';
export default async function DashboardPage() {
return (
<div>
<h1>Dashboard Page</h1>
<p>This is where I want to be redirected to.</p>
<form action={actions.printHello}>
<Button type="submit">Print Headers</Button>
</form>
</div>
);
}
Server Action (as simple as it gets):
'use server';
export async function printHello() {
console.log('****Hello Hello****');
}
If I remove the Server Action from the Dashboard page the problem goes away. I can reload the page all day long and it won’t reload the module.
Any ideas on why this could be happening?
There is a lot going on in other files that could be related, but here’s the general flow in the app:
- User logs in using the “tenant1.example.com/login” page.
- User gets a JWT access token and is redirected to the “/dashboard” page.
- When the browser comes back due to the redirect, we verify the JWT token.
- If the JWT token is valid, we “rewrite” the request to “example.com/tenant1/dashboard”.
- The “get-user-pools” module loads again for whatever reason… 🙁
Here’s the full app:
https://github.com/Gus007-BR/module-reloading/settings