-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
feat(remix-dev/vite, remix-react, remix-server-runtime): support custom basename #8145
feat(remix-dev/vite, remix-react, remix-server-runtime): support custom basename #8145
Conversation
🦋 Changeset detectedLatest commit: d9a61c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
ca9ab82
to
8039329
Compare
2236535
to
5126562
Compare
bbf2abc
to
01e201f
Compare
This is really awesome Hiroshi, as you mentioned and from my experience, it's definitely not super easy to host remix sites under a subfolder as the base url has to be manually added to every Link and redirect along with custom route generation. This will make it much easier thanks! |
Thanks for the new tests @hi-ogawa! I merged @markdalgleish Let's sync up next week on the comments above about I think for With a custom express server I'm unsure if I'm missing a capability or if we want to just recommend (or even enforce) that they're the same in dev, but let you do whatever you want in prod. |
Chatted with @markdalgleish and @pcattori and we realized that Vite's I updated the plugin such that:
It's slightly confusing to see
|
I think using |
cound basename option be configured differently between client and server-side? because sometimes the reverse proxy server has path-rewrite option. |
Remix/React-router needs to manage a whole user-visible URL in the same way both on client and server, so I don't think your reverse proxy scenario is supported at the remix level. Though I'm not too familiar with reverse proxy or path-rewrite, I would guess removing such path-rewrite option and letting remix app handle full url pathname would be a simpler way to make it work. If reverse proxy rewrite is unavoidable for some reason, then I think it's still possible to compensate for that at your (express) custom server level, something like: const app = express();
// do whatever necessary so that remix handler will see `req.url` with `basename`
app.use((req, _res, next) => {
req.url = "/mybase" + req.url;
next();
})
// normal remix request handler
app.all("*", createRequestHandler({ ... })) |
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
Thanks for this - in vite.config.ts we're using base:"/shopify-setup-app/",
plugins: [
remix({basename: "/shopify-setup-app/",...})
] and works nicely. Just need some docs now! 😊 |
In case you haven't found them - we do have docs for |
Need some domain knowledge here, @brophdawg11: it looks like when the
Works fine with any other values for Can you help me to understand whether it's a Remix plugin issue or Vite so that I can raise an issue accordingly? Issue details described in this discussion: #9375. |
Closes: #8052
References: #2891, #8077 (comment), #5236
This PR further extends #8141 to integrate react-router's
basename
option so that route definition and navigation urls don't require to be manually prefixed with custom basename.Maybe I should've discussed with Remix team beforehand, but I thought knowing the amount of change required for this feature can be an important factor, so here it is.
Please let me know if there is a concern about the introduction of this feature (and/or a concern about this PR's approach).
Thanks!
summary of the changes
RemixVitePluginOptions.basename
ServerBuild.basename
so that server-runtime can use it forcreateStaticHandler(..., { basename })
and also for a few logic outside of react-router (e.g. "X-Remix-Redirect").__remixContext.basename
so that client can use it forcreateBrowserRouter(..., { basename })
.matchRoutes(routes, url)
withmatchRoutes(routes, url, basename)
.Also the changes from #8141 are:
req.url
withreq.originalUrl
since react-router'sStaticHandler
assumes request url includes basename.publicPath
prefix to all client-visible url for vite dev.questions
I noticed
v7_prependBasename
is introduced in react-router#10336. As far as my manual test goes (e.g. relative link, useNavigate, etc...), it seems to be working without this flag, but I could be missing something.For now, I'm only testing with Vite plugin. It might be possible to support non-Vite Remix, but I haven't attempted that yet. Please let me know if it's desired to add support for non-Vite use cases as well.
Interaction between
basename
andpublicPath
options might be tricky. I was testingbasename != publicPath
case, but during dev, Vite server forces SSR request to come under the path ofbase
(= publicPath
) option, so it's required to havebasename.startsWith(publicPath)
. On the other hand, I believe there's no restriction for "build + start" as long as the server is configured to handle the routes properly. So, I added this exact validation inresolvePluginConfig
, but probably we can explore other approaches as well (for example, ifpublicPath
is undefined butbasename
is given, then remix automatically setspublicPath = basename
assuming that's the most common usage).testing strategy
I setup a demo project in hi-ogawa/remix-vite-custom-base#1 and wrote a list of features I'm testing manually.
For playwright test here, I only added very simple one as a reference. If the feature is agreed upon, then I plan to expand the test cases.