Skip to content
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

Merged
merged 45 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
148401b
feat(remix-dev/vite): support custom base
hi-ogawa Nov 27, 2023
d8efd2e
feat(remix-react, remix-server-runtime): support react-router basename
hi-ogawa Nov 27, 2023
2e42c32
fix: fix some internal matchRoutes
hi-ogawa Nov 27, 2023
8039329
chore: revert some
hi-ogawa Nov 27, 2023
e21c628
fix: fix matchRoutes in getStylesForUrl
hi-ogawa Nov 27, 2023
aaa22ae
fix: fix resource route match
hi-ogawa Nov 27, 2023
5af1aa5
chore: comment
hi-ogawa Nov 27, 2023
8ebf2e9
fix: fix custom server url
hi-ogawa Nov 27, 2023
5126562
test: add dev test
hi-ogawa Nov 27, 2023
ecbfaf5
fix: fix data request redirection
hi-ogawa Nov 27, 2023
dd2c4d5
fix: fix redirect url
hi-ogawa Nov 27, 2023
2e10ad2
fix: separate publicPath and basename
hi-ogawa Nov 27, 2023
c3f59aa
chore: revert some
hi-ogawa Nov 27, 2023
4d31abf
add RemixVitePluginOptions.basename
hi-ogawa Nov 27, 2023
ac7c0e0
chore: validate publicPath and basename option
hi-ogawa Nov 27, 2023
01e201f
fix: basename in getStylesForUrl
hi-ogawa Nov 27, 2023
22af8d5
Merge branch 'dev' into feat-remix-react-router-basename
markdalgleish Nov 29, 2023
b025ebe
Merge branch 'dev' into feat-remix-react-router-basename
hi-ogawa Dec 5, 2023
72746fb
Merge branch 'dev' into feat-remix-react-router-basename
brophdawg11 Jan 22, 2024
dbddda0
Minor updates
brophdawg11 Jan 22, 2024
4308395
Merge branch 'dev' into feat-remix-react-router-basename
brophdawg11 Jan 22, 2024
d20b1b5
Move basename back to optional to support current build
brophdawg11 Jan 22, 2024
f933439
Use vite's base config directly
brophdawg11 Jan 23, 2024
e43ee4f
Update E2E test to use vite base config
brophdawg11 Jan 23, 2024
23add5d
Fix vite internal config to use base
brophdawg11 Jan 23, 2024
2aacf09
refactor test to use VITE_CONFIG helper
markdalgleish Jan 23, 2024
33d0850
Use originalUrl when a basename is present
brophdawg11 Jan 23, 2024
a2e25fc
test: test "Vite / basename / express"
hi-ogawa Jan 24, 2024
fd118a7
refactor: use util
hi-ogawa Jan 24, 2024
6cfd6d3
test: test custom base build
hi-ogawa Jan 24, 2024
a556329
chore: rename
hi-ogawa Jan 24, 2024
8914d88
Merge branch 'dev' into feat-remix-react-router-basename
hi-ogawa Jan 24, 2024
933fb50
test: fix createRequestHandler
hi-ogawa Jan 24, 2024
534da8c
Merge branch 'dev' into feat-remix-react-router-basename
brophdawg11 Jan 26, 2024
6d05e2d
Fix merge confict resolution issues
brophdawg11 Jan 26, 2024
365e229
Another merge conflict issue
brophdawg11 Jan 26, 2024
1cb3321
base -> publicPath and add new basename option
brophdawg11 Jan 31, 2024
05f02c6
Merge branch 'dev' into feat-remix-react-router-basename
brophdawg11 Jan 31, 2024
872046b
Wrap up unit tests
brophdawg11 Feb 1, 2024
6e5c4e6
Add docs and changeset, minor tweaks
brophdawg11 Feb 1, 2024
e1f4cbe
Update changelogs
brophdawg11 Feb 1, 2024
82d64a8
Merge branch 'dev' into feat-remix-react-router-basename
brophdawg11 Feb 5, 2024
6627401
Always use originalUrl
brophdawg11 Feb 5, 2024
dd806ee
Fix TS issue
brophdawg11 Feb 5, 2024
d9a61c2
Update .changeset/vite-deprecate-public-path.md
brophdawg11 Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-cars-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/express": patch
---

Vite: Update the express adapter to use `req.originalUrl` when an application `basename` is present
8 changes: 8 additions & 0 deletions .changeset/vite-rr-basename.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@remix-run/dev": minor
"@remix-run/express": minor
"@remix-run/react": minor
"@remix-run/server-runtime": minor
---

Vite: Add a new `basename` option to the Vite plugin, allowing users to set the internal React Router [`basename`](https://reactrouter.com/en/main/routers/create-browser-router#basename) in order to to serve their applications underneath a subpath
8 changes: 7 additions & 1 deletion docs/future/vite.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ The following subset of Remix config options are supported:
- [appDirectory][app-directory]
- [future][future]
- [ignoredRouteFiles][ignored-route-files]
- [publicPath][public-path]
- [routes][routes]
- [serverBuildPath][server-build-path]
- [serverModuleFormat][server-module-format]
Expand All @@ -63,6 +62,10 @@ The Vite plugin also accepts the following additional options:
The path to the build directory, relative to the project root. Defaults to
`"build"`.

#### basename

An optional basename for your route paths, passed through to the React Router [`basename`][rr-basename] option. Please note that this is different from your _asset_ paths - you can configure those via the Vite [`base`][vite-base] flag.

#### buildEnd

A function that is called after the full Remix build is complete.
Expand Down Expand Up @@ -210,6 +213,7 @@ In order to align the default Remix project structure with the way Vite works, t
This also means that the following configuration defaults have been changed:

- [publicPath][public-path] defaults to `"/"` rather than `"/build/"`
- `publicPath` is also no longer something you configure directly, instead it is set internally from the Vite [`base`][vite-base] config value.
- [serverBuildPath][server-build-path] has been replaced by `serverBuildFile` which defaults to `"index.js"`. This file will be written into the server directory within your configured `buildDirectory`.

## Additional features & plugins
Expand Down Expand Up @@ -1207,3 +1211,5 @@ We're definitely late to the Vite party, but we're excited to be here now!
[cloudflare-proxy-cf]: https://github.com/cloudflare/workers-sdk/issues/4875
[cloudflare-proxy-ctx]: https://github.com/cloudflare/workers-sdk/issues/4876
[cloudflare-proxy-caches]: https://github.com/cloudflare/workers-sdk/issues/4879
[rr-basename]: https://reactrouter.com/routers/create-browser-router#basename
[vite-base]: https://vitejs.dev/config/shared-options.html#base
38 changes: 30 additions & 8 deletions integration/helpers/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

export const VITE_CONFIG = async (args: {
port: number;
viteOptions?: string;
pluginOptions?: string;
vitePlugins?: string;
viteManifest?: boolean;
Expand All @@ -26,6 +27,7 @@ export const VITE_CONFIG = async (args: {
import { unstable_vitePlugin as remix } from "@remix-run/dev";

export default defineConfig({
...${args.viteOptions ?? "{}"},
ssr: {
resolve: {
externalConditions: ${JSON.stringify(
Expand Down Expand Up @@ -136,10 +138,12 @@ export const viteRemixServe = async ({
cwd,
port,
serverBundle,
basename,
}: {
cwd: string;
port: number;
serverBundle?: string;
basename?: string;
}) => {
let nodeBin = process.argv[0];
let serveProc = spawn(
Expand All @@ -154,26 +158,38 @@ export const viteRemixServe = async ({
env: { NODE_ENV: "production", PORT: port.toFixed(0) },
}
);
await waitForServer(serveProc, { port });
await waitForServer(serveProc, { port, basename });
return () => serveProc.kill();
};

type ServerArgs = {
cwd: string;
port: number;
env?: Record<string, string>;
basename?: string;
};

const createDev =
(nodeArgs: string[]) =>
async ({ cwd, port }: ServerArgs): Promise<() => unknown> => {
let proc = node(nodeArgs, { cwd });
await waitForServer(proc, { port });
async ({ cwd, port, env, basename }: ServerArgs): Promise<() => unknown> => {
let proc = node(nodeArgs, { cwd, env });
await waitForServer(proc, { port, basename });
return () => proc.kill();
};

export const viteDev = createDev([remixBin, "vite:dev"]);
export const customDev = createDev(["./server.mjs"]);

// Used for testing errors thrown on build when we don't want to start and
// wait for the server
export const viteDevCmd = ({ cwd }: { cwd: string }) => {
let nodeBin = process.argv[0];
return spawnSync(nodeBin, [remixBin, "vite:dev"], {
cwd,
env: { ...process.env },
});
};

export const using = async (
cleanup: () => unknown | Promise<unknown>,
task: () => unknown | Promise<unknown>
Expand All @@ -185,26 +201,32 @@ export const using = async (
}
};

function node(args: string[], options: { cwd: string }) {
function node(
args: string[],
options: { cwd: string; env?: Record<string, string> }
) {
let nodeBin = process.argv[0];

let proc = spawn(nodeBin, args, {
cwd: options.cwd,
env: process.env,
env: {
...process.env,
...options.env,
},
stdio: "pipe",
});
return proc;
}

async function waitForServer(
proc: ChildProcess & { stdout: Readable; stderr: Readable },
args: { port: number }
args: { port: number; basename?: string }
) {
let devStdout = bufferize(proc.stdout);
let devStderr = bufferize(proc.stderr);

await waitOn({
resources: [`http://localhost:${args.port}/`],
resources: [`http://localhost:${args.port}${args.basename ?? "/"}`],
timeout: 10000,
}).catch((err) => {
let stdout = devStdout();
Expand Down
Loading
Loading