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): deep support for server.baseURL API from nitro in dev & prod, #56

Merged
merged 9 commits into from
Dec 27, 2023
7 changes: 7 additions & 0 deletions .changeset/early-pumas-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@vinxi/server-functions": patch
"vinxi": patch
"solid-ssr-basic": patch
---

(feat): deep support for `server.baseURL` API from nitro in dev & prod,
6 changes: 5 additions & 1 deletion examples/react/rsc/spa/app/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { ServerComponent } from "./server-component";
document.addEventListener("click", async (e) => {
console.log(sayHello, "hello");

const result = await fetchServerAction("/_server", sayHello["$$id"], []);
const result = await fetchServerAction(
import.meta.env.SERVER_BASE_URL + "/_server",
sayHello["$$id"],
[],
);
console.log(result);
// sayHello();
});
Expand Down
5 changes: 4 additions & 1 deletion examples/solid/ssr/basic/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export default function App({ assets, scripts }) {
return (
<html lang="en">
<head>
<link rel="icon" href="/favicon.ico" />
<link
rel="icon"
href={`${import.meta.env.SERVER_BASE_URL}/favicon.ico`}
/>
{assets}
</head>
<body>
Expand Down
10 changes: 8 additions & 2 deletions packages/vinxi-server-functions/client-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ export function createServerReference(fn, id, name) {
return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return "/_server" + `id=${id}&name=${name}`;
return (
import.meta.env.SERVER_BASE_URL + "/_server" + `id=${id}&name=${name}`
);
}

return Reflect.get(target, prop, receiver);
},
apply(target, thisArg, args) {
return fetchServerAction("/_server", `${id}#${name}`, args);
return fetchServerAction(
`${import.meta.env.SERVER_BASE_URL}/_server`,
`${id}#${name}`,
args,
);
},
});
}
4 changes: 3 additions & 1 deletion packages/vinxi-server-functions/server-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ export function createServerReference(fn, id, name) {
return new Proxy(fn, {
get(target, prop, receiver) {
if (prop === "url") {
return "/_server" + `id=${id}&name=${name}`;
return (
import.meta.env.SERVER_BASE_URL + "/_server" + `id=${id}&name=${name}`
);
}

return Reflect.get(target, prop, receiver);
Expand Down
4 changes: 2 additions & 2 deletions packages/vinxi/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { resolveRouterConfig, routerSchema } from "./router-modes.js";
routers?: import("./router-modes.js").RouterSchemaInput[];
name?:
string;
server?: import('nitropack').NitroConfig;
server?: Omit<import('nitropack').NitroConfig, 'handlers' | 'devHandlers' | 'publicAssets' | 'scanDirs' | 'appConfigFiles' | 'imports' | 'virtual'>;
root?: string
}} AppOptions */

/** @typedef {{
config: {
name: string;
devtools: boolean;
server: import("nitropack").NitroConfig;
server: Omit<import('nitropack').NitroConfig, 'handlers' | 'devHandlers' | 'publicAssets' | 'scanDirs' | 'appConfigFiles' | 'imports' | 'virtual'>;
routers: import("./router-mode.js").Router[];
root: string;
};
Expand Down
1 change: 1 addition & 0 deletions packages/vinxi/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export async function createBuild(app, buildConfig) {
"node-fetch-native/polyfill": require.resolve(
"node-fetch-native/polyfill",
),
...(app.config.server.alias ?? {}),
// "unstorage/drivers/fs-lite": require.resolve("unstorage/drivers/fs-lite"),
// "unstorage/drivers/fs": require.resolve("unstorage/drivers/fs"),
// defu: require.resolve("defu"),
Expand Down
9 changes: 6 additions & 3 deletions packages/vinxi/lib/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inspect } from "@vinxi/devtools";
import { fileURLToPath } from "node:url";

import { consola, withLogger } from "./logger.js";
import { normalize } from "./path.js";
import { join, normalize } from "./path.js";

export * from "./router-dev-plugins.js";

Expand Down Expand Up @@ -57,9 +57,12 @@ export async function createViteHandler(router, app, serveConfig) {
...(((await router.plugins?.(router)) ?? []).filter(Boolean) || []),
].filter(Boolean);

let base = join(app.config.server.baseURL ?? "/", router.base);

console.log(base);
const viteDevServer = await createViteDevServer({
configFile: false,
base: router.base,
base,
plugins,
optimizeDeps: {
force: serveConfig.force,
Expand Down Expand Up @@ -122,7 +125,7 @@ export async function createDevServer(
publicAssets: [
...app.config.routers
.map((router) => {
return router.internals.mode.dev.publicAssets?.(router, app.config);
return router.internals.mode.dev.publicAssets?.(router, app);
})
.filter(
/**
Expand Down
12 changes: 7 additions & 5 deletions packages/vinxi/lib/manifest/dev-server-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function createDevManifest(app) {

let router = app.getRouter(bundlerName);

let base = join(app.config.server.baseURL ?? "", router.base);

if (router.mode === "static") {
return {
json() {
Expand All @@ -27,7 +29,7 @@ export function createDevManifest(app) {
routes() {
return [];
},
base: router.base,
base,
target: "static",
mode: router.mode,
handler: undefined,
Expand Down Expand Up @@ -64,7 +66,7 @@ export function createDevManifest(app) {
return {};
},
handler: router.handler,
base: router.base,
base,
target: router.target,
mode: router.mode,
chunks: new Proxy(
Expand All @@ -83,7 +85,7 @@ export function createDevManifest(app) {
if (router.target === "browser") {
return {
output: {
path: join(router.base, "@fs", absolutePath),
path: join(base, "@fs", absolutePath),
},
};
} else {
Expand Down Expand Up @@ -234,15 +236,15 @@ export function createDevManifest(app) {
attrs: {
key: "vite-client",
type: "module",
src: join(router.base, "@vite", "client"),
src: join(base, "@vite", "client"),
},
},
]
: []),
].filter(Boolean);
},
output: {
path: join(router.base, "@fs", absolutePath),
path: join(base, "@fs", absolutePath),
},
};
} else {
Expand Down
50 changes: 31 additions & 19 deletions packages/vinxi/lib/manifest/prod-server-manifest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { joinURL } from "ufo";
import invariant from "vinxi/lib/invariant";
import { handlerModule, join, virtualId } from "vinxi/lib/path";

Expand All @@ -7,21 +8,19 @@ import findAssetsInViteManifest from "./vite-manifest.js";

/** @typedef {import("../app.js").App & { config: { buildManifest: { [key:string]: any } }}} ProdApp */

function createHtmlTagsForAssets(router, assets) {
return assets.filter(
(asset) =>
asset.endsWith(".css") || asset.endsWith(".js"),
)
.map((asset) => ({
tag: "link",
attrs: {
href: join(router.base, asset),
key: join(router.base, asset),
...(asset.endsWith(".css")
? { rel: "stylesheet", precendence: "high" }
: { rel: "modulepreload" }),
},
}));
function createHtmlTagsForAssets(router, app, assets) {
return assets
.filter((asset) => asset.endsWith(".css") || asset.endsWith(".js"))
.map((asset) => ({
tag: "link",
attrs: {
href: joinURL(app.config.server.baseURL ?? "", router.base, asset),
key: join(app.config.server.baseURL ?? "", router.base, asset),
...(asset.endsWith(".css")
? { rel: "stylesheet", precendence: "high" }
: { rel: "modulepreload" }),
},
}));
}

/**
Expand Down Expand Up @@ -121,7 +120,11 @@ export function createProdManifest(app) {
: input;
return {
assets() {
return createHtmlTagsForAssets(router, findAssetsInViteManifest(bundlerManifest, id));
return createHtmlTagsForAssets(
router,
app,
findAssetsInViteManifest(bundlerManifest, id),
);
},
output: {
path: join(
Expand All @@ -139,17 +142,26 @@ export function createProdManifest(app) {
return {
import() {
return import(
/* @vite-ignore */ join(
/* @vite-ignore */ joinURL(
app.config.server.baseURL ?? "",
router.base,
bundlerManifest[id].file,
)
);
},
assets() {
return createHtmlTagsForAssets(router, findAssetsInViteManifest(bundlerManifest, id));
return createHtmlTagsForAssets(
router,
app,
findAssetsInViteManifest(bundlerManifest, id),
);
},
output: {
path: join(router.base, bundlerManifest[id].file),
path: joinURL(
app.config.server.baseURL ?? "",
router.base,
bundlerManifest[id].file,
),
},
};
}
Expand Down
5 changes: 4 additions & 1 deletion packages/vinxi/lib/nitro-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ export function createDevServer(nitro) {

// Dev-only handlers
for (const handler of nitro.options.devHandlers) {
app.use(handler.route || "/", handler.handler);
app.use(
joinURL(nitro.options.runtimeConfig.app.baseURL, handler.route ?? "/"),
handler.handler,
);
}

// User defined dev proxy
Expand Down
2 changes: 1 addition & 1 deletion packages/vinxi/lib/plugins/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
* @param {string} tag
* @param {Omit<import('vite').InlineConfig, 'router'>} conf
* @param {import('../vite-dev.d.ts').CustomizableConfig} conf
* @returns {import('../vite-dev.d.ts').Plugin}
*/
export function config(tag, conf) {
Expand Down
7 changes: 7 additions & 0 deletions packages/vinxi/lib/plugins/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export function manifest() {
"import.meta.env.ROUTER_NAME": JSON.stringify(router.name),
"import.meta.env.ROUTER_HANDLER": JSON.stringify(router.handler),
"import.meta.env.CWD": JSON.stringify(router.root),
"import.meta.env.SERVER_BASE_URL": JSON.stringify(
app.config.server.baseURL ?? "",
),
"import.meta.env.ROUTERS": JSON.stringify(
app.config.routers.map((router) => router.name),
),
Expand Down Expand Up @@ -67,10 +70,13 @@ export function manifest() {
export function injectVinxiClient() {
/** @type {import('../router-mode.js').Router} */
let router;
/** @type {import('../app.js').App} */
let app;
return {
name: "vinxi:inject-client-runtime",
configResolved(config) {
router = config.router;
app = config.app;
},
apply: "serve",
transformIndexHtml(html) {
Expand All @@ -80,6 +86,7 @@ export function injectVinxiClient() {
attrs: {
type: "module",
src: join(
app.config.server.baseURL ?? "",
router.base,
"@fs",
`${fileURLToPath(
Expand Down
25 changes: 24 additions & 1 deletion packages/vinxi/lib/vite-dev.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Plugin as VitePlugin, ResolvedConfig as _ResolvedConfig } from "vite";

import { App } from "./app.js";
import { Router } from "./router-mode.js";
import { DevConfig } from "./dev-server.js";
import { Router } from "./router-mode.js";

declare module "vite" {
interface UserConfig {
Expand All @@ -28,4 +28,27 @@ export type ViteConfig = _ResolvedConfig & { router: Router; app: App };

export type Plugin = VitePlugin;

export type CustomizableConfig = Omit<
import("vite").InlineConfig,
| "appType"
| "app"
| "router"
| "base"
| "root"
| "publicDir"
| "mode"
| "server"
| "preview"
| "clearScreen"
| "configFile"
| "envFile"
> & {
build?: Omit<
import("vite").InlineConfig["build"],
"outDir" | "ssr" | "ssrManifest" | "rollupOptions"
> & {
rollupOptions?: Omit<import("vite").BuildOptions["rollupOptions"], "input">;
};
};

export type { ConfigEnv as ConfigEnv } from "vite";
Loading