From 4d386d822ab8b3abd35217f474c1ae0f65d64fc0 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Sun, 3 Jul 2022 11:43:23 +1000 Subject: [PATCH 1/5] Help users that visit the Vite dev server directly --- package.json | 2 +- src/dev-server-index.html | 77 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 23 ++++++++++-- 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/dev-server-index.html diff --git a/package.json b/package.json index e51a39b..aa5e725 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "scripts": { "build": "npm run build-plugin && npm run build-inertia-helpers", - "build-plugin": "rm -rf dist && tsc", + "build-plugin": "rm -rf dist && tsc && cp src/dev-server-index.html dist/", "build-inertia-helpers": "rm -rf inertia-helpers && tsc --project tsconfig.inertia-helpers.json", "lint": "eslint --ext .ts ./src ./tests", "ssr:serve": "vite build --ssr && node storage/ssr/ssr.js", diff --git a/src/dev-server-index.html b/src/dev-server-index.html new file mode 100644 index 0000000..a776cb4 --- /dev/null +++ b/src/dev-server-index.html @@ -0,0 +1,77 @@ + + + + + + Laravel Vite + + + + + + + + +
+
+
+ +
+
+

This is the Vite development server that provides Hot Module Replacement for your Laravel application.

+

To access your Laravel application, you will need to run a local development server.

+

Laravel Sail

+

A light-weight command-line interface for interacting with Laravel's default Docker development environment.

+

Laravel Valet

+

A development environment for macOS minimalists.

+

Artisan Serve

+

Laravel's local development server built on PHP's built-in web server.

+
+
+

+ Your Laravel application's configured APP_URL is:
+ {{ APP_URL }} +

+

Want more information on Laravel's Vite integration?

+

+ Read the docs → +

+
+
+
+
+
+ + diff --git a/src/index.ts b/src/index.ts index cbe2f5a..2406ae8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -164,6 +164,9 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug configureServer(server) { const hotFile = path.join(pluginConfig.publicDirectory, 'hot') + const envDir = resolvedConfig.envDir || process.cwd() + const appUrl = loadEnv('', envDir, 'APP_URL').APP_URL + server.httpServer?.once('listening', () => { const address = server.httpServer?.address() @@ -177,9 +180,6 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug viteDevServerUrl = `${protocol}://${host}:${address.port}` fs.writeFileSync(hotFile, viteDevServerUrl) - const envDir = resolvedConfig.envDir || process.cwd() - const appUrl = loadEnv('', envDir, 'APP_URL').APP_URL - setTimeout(() => { server.config.logger.info(colors.red(`\n Laravel ${laravelVersion()} `)) server.config.logger.info(`\n > APP_URL: ` + colors.cyan(appUrl)) @@ -203,6 +203,23 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug process.on('SIGHUP', process.exit) exitHandlersBound = true + + return () => { + server.middlewares.use((req, res, next) => { + if (req.url === '/index.html') { + server.config.logger.warn( + "\n" + colors.bgYellow( + colors.black(`The Vite server should not be accessed directly. Please visit ${appUrl} instead.`) + ) + ) + res.end( + fs.readFileSync(path.join(__dirname, 'dev-server-index.html')).toString().replace(/{{ APP_URL }}/g, appUrl) + ) + } + + next() + }); + } }, // The following two hooks are a workaround to help solve a "flash of unstyled content" with Blade. From 81132b735d2ac99dde1301e0d618cfe92b2b39b9 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 4 Jul 2022 17:08:19 +1000 Subject: [PATCH 2/5] send 404 response status --- src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index.ts b/src/index.ts index 2406ae8..67b4e74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -212,6 +212,9 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug colors.black(`The Vite server should not be accessed directly. Please visit ${appUrl} instead.`) ) ) + + res.statusCode = 404 + res.end( fs.readFileSync(path.join(__dirname, 'dev-server-index.html')).toString().replace(/{{ APP_URL }}/g, appUrl) ) From 94d14a72ebc6ba97ef43f710c2babd41544382ea Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Tue, 5 Jul 2022 09:27:44 +1000 Subject: [PATCH 3/5] Update console wording Co-authored-by: Tim MacDonald --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 67b4e74..867c58d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -209,7 +209,7 @@ function resolveLaravelPlugin(pluginConfig: Required): LaravelPlug if (req.url === '/index.html') { server.config.logger.warn( "\n" + colors.bgYellow( - colors.black(`The Vite server should not be accessed directly. Please visit ${appUrl} instead.`) + colors.black(`The Vite server should not be accessed directly. Your Laravel application's configured APP_URL is: ${appUrl}`) ) ) From e069a206bad1a9b5d78a985bf11300cc2e035c29 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Tue, 5 Jul 2022 09:36:56 +1000 Subject: [PATCH 4/5] Formatting --- src/dev-server-index.html | 21 +++++++++++---------- src/index.ts | 28 +++++++++++++--------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/dev-server-index.html b/src/dev-server-index.html index a776cb4..9ddef54 100644 --- a/src/dev-server-index.html +++ b/src/dev-server-index.html @@ -8,6 +8,7 @@ +