Open jbnv opened 4 years ago
Hi @jbnv,
It looks like nuxt is run in development here.
In development nuxt uses a virtual filesystem to avoid problems with HMR (hot module reloading).
If you want the spa.html file to be generated you have to use the nuxt generate
or nuxt build
cli command (they are equivalent in mode: 'spa'
), or their respective programmatic counterparts.
Could you share your approach in a bit more detail (e.g. content of your server.js
file), i'd be curious to see your implementation.
I'm a bit confused as to what role Laravel has in serving the HTML. Per the instructions here, you create a catch-all route to have Laravel return HTML that runs your SPA. I already had such a route, and it created my page from a blade, so I figured the replacement was fairly simple:
Route::get(
'/{path?}',
function() { return file_get_contents(public_path('spa.html')); }
)->where('path', '.*')->name('nuxt');
Obviously this depends on something writing public/spa.html
.
My server.js is rather basic:
async function start() {
const nuxt = new Nuxt(config);
const { host, port } = nuxt.options.server;
await nuxt.ready();
if (config.dev) {
const builder = new Builder(nuxt);
await builder.build();
}
app.use(nuxt.render);
app.listen(port, host);
console.log(`Server listening on http://${host}:${port}.`);
}
start();
A big hole in my understanding is what the server and serves.
The route you created uses a fixed reference to a static file. The whole point of the module is to setup a proxy connection between the Laravel test server and the Nuxt test server.
Checkout the Documentation on Route forwarding:
Notice that the file_get_contents(env('NUXT_OUTPUT_PATH', public_path('spa.html'))
first attempts to serve a file specified by an env variable before falling back to the static file.
In development this module injects the env variable NUXT_OUTPUT_PATH
containing the URL of the testserver, which overrides the static file.
If you don't need any specific customization of the server I'd recommend using the inbuild commands of nuxt to start the testserver/build.
i.e. instead of using cross-env NODE_ENV=development nodemon server.js --watch server
in your script you can simply use nuxt
(for development) or nuxt build
to build the static files.
On the topic of the two servers:
Laravel has a testserver which uses the buildin php server (php artisan serve
).
Nuxt has a node based testserver which loads compiled files into memory to better handle HMR (Hot Module Reloading), this test server is not connected to laravel in any way (e.g. a laravel session would not propagate to the testserver)
In order to circumvent this limitation the module starts the laravel testserver from inside node and injects some env variables which can be used by the PHP configuration:
APP_URL
are resolved correctly by laravelAdditionally in development @nuxtjs/axios
is configured to proxy all requests to laravel except for the special template URL
The Nuxt server never generates NUXT_OUTPUT_PATH, or at least never writes NUXT_OUTPUT_PATH to .env
. And am I supposed to set APP_URL myself or does one of the servers generate it?
Additional information: Nuxt indicates that Laravel support is enabled, and it starts the Laravel development server. That's the only indication that I get that any of this plugin actually ran.
@jbnv
The module does not write to the .env file when used with nuxt dev
, it injects the 2 variables mentioned above into the environment of the process, started from node (using execa
=> wrapper for child_process
) to start the laravel testserver.
Thus the variables defined in .env
are extended/overwritten, i.e. you don't need to have them in the .env file.
Can you not access the page in development (localhost:3000
)?
Could you post the error that you get (js errors from the browser console and/or php errors from the storage/logs/*.log
file)?
In production the nuxt sources are not served through a node server but compiled into actual JS, therefore there is no underlying injection of env variables into an environment.
All env variables are read from the current environment + the .env
file.
This module provides the dotEnvExport
configuration property as convenience, so that if true
, the module writes to the .env
file once all JS files have been compiled successfully (nuxt build
or nuxt generate
)
I have no idea what I'm doing wrong. The Nuxt server isn't producing the
spa.html
file. It's not throwing any error messages either:Here's my
nuxt.config.js
object, stripped down to remove all other dependencies: