Next.js Discord

Discord Forum

Multi-tenant Next.js app without a root /app/layout.js

Unanswered
Japanese Bobtail posted this in #help-forum
Open in Discord
Japanese BobtailOP
Hmm so I'm implementing multi-tenancy on my application a la https://github.com/vercel/platforms
My base layout requires some tenant-specific logic in it and I'm actually surprised to find that the app appears to work perfectly fine without a /app/layout.js file and just have /app/[tenant]/layout.js in its place.
Middleware is implemented (as it is in that example repo) to rewrite all requests with the tenant prefixed in the path, based on the hostname the user is accessing the app from.

Is there any reason it would be a bad idea to do away with /app/layout.js entirely?

8 Replies

American black bear
if it works perfectly fine without it I don't seem the problem of not having a "root" rootlayout, however I would still keep it to render html and body, then create a sub layout in your tenant route with the thenant logic
src/
  app/
    # contains root layout logic
    layout.tsx
    [tenant]
      # contains tenant-specific logic
      layout.tsx
Japanese BobtailOP
The conditional logic was for <script /> tags in the <head>
And I mean I know that <Script /> does some sorcery to control where it actually gets rendered in the dom, despite where you have it in your react component tree
But it just felt really clean to have the whole layout in /app/[tenant]/layout.js
At first I turned app/layout.js into ({ children }) => children
But then my boss was like, “what if you just remove it entirely?”
And I was surprised it seems to work fine
It works fine, that’s a valid implementation. The thing is you’ll get a full page load when navigating between pages living in different tenants, if that’s not an issue for you then you’re fine.

When you have multiple root layouts it’s like you’re basically remounting the whole app when you switch between different sections that share different root layouts.