-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SSR asPath behavior when mounting app under a path #4792
Comments
Unrelated to this issue, but you're missing:
|
Actually, I don't want to serve anything under the root. I plan to compose several of these together with |
Oh wait nvm you want to only server un /test 👍 |
For context, I'm just in the experimentation phase of a new i18n workflow I'd like to try.
edit: server.use('/static', handle) ? |
It's actually a problem with express, they override https://expressjs.com/en/api.html#req.originalUrl
|
Yes, they have |
So what you have to do is |
This will also make sure Next looks for the right directory, as we don't have a client-side basepath option. Meaning your |
I tried also, I have a solution for the // components/link.js
import Link from 'next/link';
import React from 'react';
import PropTypes from 'prop-types';
export default class PrefixedLink extends React.Component {
render () {
const { href, as = href, ...props } = this.props;
const { prefix = '' } = this.context;
return <Link href={href} as={`${prefix}${as}`} {...props} />;
}
}
PrefixedLink.contextTypes = {
prefix: PropTypes.string
};
export class PathPrefix extends React.Component {
getChildContext () {
return { prefix: this.props.prefix || '' };
}
render () {
return this.props.children;
}
}
PathPrefix.childContextTypes = {
prefix: PropTypes.string
}; Just make sure to set the context in // pages/_app.js
import App, { Container } from 'next/app';
import React from 'react';
import { PathPrefix } from '../components/link';
export default class MyApp extends App {
static async getInitialProps ({ Component, router, ctx }) {
let pageProps = Component.getInitialProps
? await Component.getInitialProps(ctx)
: {};
return { pageProps };
}
render () {
const { Component, pageProps } = this.props;
return <Container>
<PathPrefix prefix='/test'}>
<Component {...pageProps} />
</PathPrefix>
</Container>;
}
} (next.js doesn't work with the new React context API) |
All your pages should be put into |
hmmm, that seems to generate some errors
edit: server.get(`/test*`, handle); But that doesn't really solve the problem |
@timneutkens Actually, the following seems to do it, when using server.use('/test', (req, res) => {
if (!/^\/(?:static|_next)\//.test(req.url)) {
req.url = req.originalUrl;
}
handle(req, res);
}); as well as server.get('/test*', (req, res) => {
const nextUrl = req.url.replace(/^\/test/, '');
if (/^\/(?:static|_next)\//.test(nextUrl)) {
req.url = nextUrl;
}
handle(req, res);
}); but tbh, it feels a bit hacky. I mean, whether using |
In my ideal world I'd like to be able to do const app = next({
dev,
conf: {
// tell next where the app root is (so it can strip it from incoming urls on the server)
// will also be passed to clientside router for <Link>s
appPrefix: `/test`,
assetPrefix: `/test` // or a cdn
}
}); And server.get('/test*', app.getRequestHandler()); And the pages would reside under |
@Janpot Tried your method above and I got a 404 for all my _next files.
|
And you've put your pages under |
I did not. I think that is the issue... will report back once my build process finishes. Thanks! |
@Janpot So for localhost:3000/auth/${PATH} I get: My pages are now inside the auth subfolder. Anything else I am missing? |
@lucasnorman Check out this branch: https://github.com/Janpot/next-mount-subpath/tree/with-subfolder @timneutkens Do you mean that with this solution, |
Closing this in favor of #4998 |
Bug report
Describe the bug
When mounting the next.js handler under a path
asPath
shows different values on server and browser.To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
npm i
npm run dev
http://localhost:3000/test
Expected behavior
No warnings
Screenshots
System information
The text was updated successfully, but these errors were encountered: