Comments (6)
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.
from nuxt-laravel.
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.
from nuxt-laravel.
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:
- NUXT_OUTPUT_PATH:
- In development: contains a special URL to the nuxt testserver which exposes the generated template, effectively "proxying" calls over Laravel into Nuxt
- In production: fetches the contents form the nuxt generated template, effectively serving the files generated by nuxt
- APP_URL:
- In development: contains the URL to the nuxt testserver, so that references to
APP_URL
are resolved correctly by laravel - In production: unchanged
- In development: contains the URL to the nuxt testserver, so that references to
Additionally in development @nuxtjs/axios
is configured to proxy all requests to laravel except for the special template URL
from nuxt-laravel.
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?
from nuxt-laravel.
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.
from nuxt-laravel.
@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
)
from nuxt-laravel.
Related Issues (14)
- Windows path problem HOT 3
- Axios uses wrong port HOT 19
- Route [nuxt] not defined.
- Rendering meta tags in server side while there is no SSR by now
- Wrong named output file in publicDir HOT 2
- Missing closing bracket in readme
- Added Support to Nuxt Content
- 'render-before' hook deletes entire public directory HOT 3
- Strange behavior with PHP 7.4 HOT 1
- SSR HOT 9
- Add Laravel to Nuxt HOT 2
- Rendering everything to one JS file HOT 2
- How to access to cookies and sessions from nuxt side HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nuxt-laravel.