@netlify/vite-plugin-react-router 3.1.0-next.0 → 3.1.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -120,11 +120,12 @@ var import_promises = require("fs/promises");
120
120
  var import_node_path2 = require("path");
121
121
  var import_posix = require("path/posix");
122
122
  var import_vite_plugin = __toESM(require("@netlify/vite-plugin"));
123
+ var import_node_fetch_server = require("@remix-run/node-fetch-server");
123
124
  var import_tinyglobby = require("tinyglobby");
124
125
 
125
126
  // package.json
126
127
  var name = "@netlify/vite-plugin-react-router";
127
- var version = "3.1.0-next.0";
128
+ var version = "3.1.0-next.2";
128
129
 
129
130
  // src/lib/rollup.ts
130
131
  var import_node_path = require("path");
@@ -239,6 +240,7 @@ function netlifyPlugin(options = {}) {
239
240
  let isProductionSsrBuild = false;
240
241
  let currentCommand;
241
242
  let isHydrogenSite = false;
243
+ let userServerFile;
242
244
  const reactRouterPlugin = {
243
245
  name: "vite-plugin-netlify-react-router",
244
246
  config(_config, { command, isSsrBuild }) {
@@ -307,14 +309,52 @@ function netlifyPlugin(options = {}) {
307
309
  async handler(config) {
308
310
  resolvedConfig = config;
309
311
  isHydrogenSite = config.plugins.some((plugin) => plugin.name === "hydrogen:main");
310
- if (isHydrogenSite && edge && isProductionSsrBuild) {
311
- const userServerFile = await findUserEdgeFunctionHandlerFile(config.root);
312
- if (config.build?.rollupOptions?.input && typeof config.build.rollupOptions.input === "object" && !Array.isArray(config.build.rollupOptions.input)) {
313
- config.build.rollupOptions.input[FUNCTION_HANDLER_CHUNK] = userServerFile;
312
+ if (isHydrogenSite && edge) {
313
+ userServerFile = await findUserEdgeFunctionHandlerFile(config.root);
314
+ if (isProductionSsrBuild) {
315
+ if (config.build?.rollupOptions?.input && typeof config.build.rollupOptions.input === "object" && !Array.isArray(config.build.rollupOptions.input)) {
316
+ config.build.rollupOptions.input[FUNCTION_HANDLER_CHUNK] = userServerFile;
317
+ }
314
318
  }
315
319
  }
316
320
  }
317
321
  },
322
+ // In dev, Hydrogen sites need their server.ts to be loaded and called for each request so that
323
+ // it can provide `getLoadContext` (storefront, cart, session, etc.) to React Router's request
324
+ // handler. Without this, React Router's dev middleware would handle SSR with no load context.
325
+ configureServer: {
326
+ order: "pre",
327
+ handler(viteDevServer) {
328
+ if (!isHydrogenSite || !edge) return;
329
+ if (!userServerFile) {
330
+ viteDevServer.config.logger.warn(
331
+ "Hydrogen site detected but no server.ts found. Dev SSR will fall through to React Router defaults."
332
+ );
333
+ return;
334
+ }
335
+ const serverEntryFile = userServerFile;
336
+ return () => {
337
+ viteDevServer.middlewares.use(async (req, res, next) => {
338
+ try {
339
+ const serverModule = await viteDevServer.ssrLoadModule((0, import_node_path2.join)(viteDevServer.config.root, serverEntryFile));
340
+ const handler = serverModule.default;
341
+ req.url = req.originalUrl ?? req.url;
342
+ const request = (0, import_node_fetch_server.createRequest)(req, res);
343
+ const netlifyContext = { waitUntil: () => {
344
+ }, ...globalThis.Netlify?.context };
345
+ const response = await handler(request, netlifyContext);
346
+ if (response) {
347
+ await (0, import_node_fetch_server.sendResponse)(res, response);
348
+ } else {
349
+ next();
350
+ }
351
+ } catch (error) {
352
+ next(error);
353
+ }
354
+ });
355
+ };
356
+ }
357
+ },
318
358
  // See https://rollupjs.org/plugin-development/#writebundle.
319
359
  async writeBundle() {
320
360
  if (isProductionSsrBuild) {
package/dist/index.mjs CHANGED
@@ -9,11 +9,12 @@ import { access, mkdir, writeFile } from "node:fs/promises";
9
9
  import { dirname, join, relative, resolve, sep } from "node:path";
10
10
  import { sep as posixSep } from "node:path/posix";
11
11
  import netlifyVitePlugin from "@netlify/vite-plugin";
12
+ import { createRequest, sendResponse } from "@remix-run/node-fetch-server";
12
13
  import { glob } from "tinyglobby";
13
14
 
14
15
  // package.json
15
16
  var name = "@netlify/vite-plugin-react-router";
16
- var version = "3.1.0-next.0";
17
+ var version = "3.1.0-next.2";
17
18
 
18
19
  // src/lib/rollup.ts
19
20
  import { basename, extname } from "node:path";
@@ -128,6 +129,7 @@ function netlifyPlugin(options = {}) {
128
129
  let isProductionSsrBuild = false;
129
130
  let currentCommand;
130
131
  let isHydrogenSite = false;
132
+ let userServerFile;
131
133
  const reactRouterPlugin = {
132
134
  name: "vite-plugin-netlify-react-router",
133
135
  config(_config, { command, isSsrBuild }) {
@@ -196,14 +198,52 @@ function netlifyPlugin(options = {}) {
196
198
  async handler(config) {
197
199
  resolvedConfig = config;
198
200
  isHydrogenSite = config.plugins.some((plugin) => plugin.name === "hydrogen:main");
199
- if (isHydrogenSite && edge && isProductionSsrBuild) {
200
- const userServerFile = await findUserEdgeFunctionHandlerFile(config.root);
201
- if (config.build?.rollupOptions?.input && typeof config.build.rollupOptions.input === "object" && !Array.isArray(config.build.rollupOptions.input)) {
202
- config.build.rollupOptions.input[FUNCTION_HANDLER_CHUNK] = userServerFile;
201
+ if (isHydrogenSite && edge) {
202
+ userServerFile = await findUserEdgeFunctionHandlerFile(config.root);
203
+ if (isProductionSsrBuild) {
204
+ if (config.build?.rollupOptions?.input && typeof config.build.rollupOptions.input === "object" && !Array.isArray(config.build.rollupOptions.input)) {
205
+ config.build.rollupOptions.input[FUNCTION_HANDLER_CHUNK] = userServerFile;
206
+ }
203
207
  }
204
208
  }
205
209
  }
206
210
  },
211
+ // In dev, Hydrogen sites need their server.ts to be loaded and called for each request so that
212
+ // it can provide `getLoadContext` (storefront, cart, session, etc.) to React Router's request
213
+ // handler. Without this, React Router's dev middleware would handle SSR with no load context.
214
+ configureServer: {
215
+ order: "pre",
216
+ handler(viteDevServer) {
217
+ if (!isHydrogenSite || !edge) return;
218
+ if (!userServerFile) {
219
+ viteDevServer.config.logger.warn(
220
+ "Hydrogen site detected but no server.ts found. Dev SSR will fall through to React Router defaults."
221
+ );
222
+ return;
223
+ }
224
+ const serverEntryFile = userServerFile;
225
+ return () => {
226
+ viteDevServer.middlewares.use(async (req, res, next) => {
227
+ try {
228
+ const serverModule = await viteDevServer.ssrLoadModule(join(viteDevServer.config.root, serverEntryFile));
229
+ const handler = serverModule.default;
230
+ req.url = req.originalUrl ?? req.url;
231
+ const request = createRequest(req, res);
232
+ const netlifyContext = { waitUntil: () => {
233
+ }, ...globalThis.Netlify?.context };
234
+ const response = await handler(request, netlifyContext);
235
+ if (response) {
236
+ await sendResponse(res, response);
237
+ } else {
238
+ next();
239
+ }
240
+ } catch (error) {
241
+ next(error);
242
+ }
243
+ });
244
+ };
245
+ }
246
+ },
207
247
  // See https://rollupjs.org/plugin-development/#writebundle.
208
248
  async writeBundle() {
209
249
  if (isProductionSsrBuild) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/vite-plugin-react-router",
3
- "version": "3.1.0-next.0",
3
+ "version": "3.1.0-next.2",
4
4
  "description": "React Router 7+ Vite plugin for Netlify",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -54,7 +54,8 @@
54
54
  "dependencies": {
55
55
  "@netlify/edge-functions": "^3.0.4",
56
56
  "@netlify/functions": "^5.1.3",
57
- "@netlify/vite-plugin": "^2.10.11",
57
+ "@netlify/vite-plugin": "^2.11.0",
58
+ "@remix-run/node-fetch-server": "^0.9.0",
58
59
  "isbot": "^5.1.25",
59
60
  "tinyglobby": "^0.2.10"
60
61
  },
@@ -65,7 +66,7 @@
65
66
  "react-dom": "^18.2.0",
66
67
  "react-router": "^7.9.4",
67
68
  "tsup": "^8.0.2",
68
- "vite": "^6.2.5"
69
+ "vite": "^8.0.0"
69
70
  },
70
71
  "peerDependencies": {
71
72
  "react-router": ">=7.9.0",