-
Notifications
You must be signed in to change notification settings - Fork 24
Support for vite configuration #360
Comments
@rellafella when you access the site directly, can you inspect the headers in the network tab and provide the output? |
Maybe you'll find some hints here? https://nystudio107.com/docs/vite/#using-nitro As for CORS, Vite by default: https://vitejs.dev/config/#server-cors Sets it up to allow access from any origin. So your error is a little puzzling. This really shouldn't be any different than setup for |
Response headers (app.js)HTTP/1.1 200 OK
This is what has me puzzled too, I have tried playing around with the server.cors options too with no luck.
I'll see how I go trying to incorporate Andrew Menich's dev server config from mix and let you know on here if there is anything different. |
Update: That was following almost an identical configuration, obviously leveraging Twigpack instead though. But still the same process of So still a bit perplexed. I am getting many more headers from the webpackDevserver files compared to the Vite dev server. Response headers (app.js)
|
I think Vite might use a web socket for some of this -- not sure how that'd impact anything tho. |
I am not really sure what to do with this information but it might be useful to the issue. In Andrew M's article he recommends using curl to debug the location of the webpack manifest so out of interest I thought I would try a similar thing with Vite. If I run /app $ curl -I 'http://localhost:3000/src/js/app.js'
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/javascript
Content-Length: 351
Last-Modified: Mon, 24 May 2021 02:23:33 GMT
ETag: W/"351-1621823013222"
Cache-Control: no-cache
Date: Mon, 24 May 2021 02:36:43 GMT
Connection: keep-alive
Keep-Alive: timeout=5 |
@rellafella set: 'checkDevServer' => false, I'm pretty sure that what's happening is since you have that on, it's testing: 'devServerInternal' => App::env('VITE_DEV_SERVER_PUBLIC'), ...to see if the Vite dev server is running. Since you have it set to:
...this is going to fail, because it's doing this inside of the Docker network. The address in there will not be the same as the So turn off For my Docker setup for example, it looks like this:
|
Have tried toggling |
It definitely will have an effect... if you tell it to check for the dev server, and it can't connect to it, it will fall back on the production build. |
Sorry, yes in that respect it does have an effect, but it doesn't seem to change anything for the CORS issue. I have tried a few different values for |
I've been summoned! I was able to get HMR working with Nitro and Vite. Below is the config settings @rellafella posted, modified to match what has worked for me:
import vue from '@vitejs/plugin-vue';
import ViteRestart from 'vite-plugin-restart';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import path from 'path';
// https://vitejs.dev/config/
export default ({ command }) => ({
base: command === 'serve' ? '' : '/dist/',
build: {
manifest: true,
outDir: './web/dist',
rollupOptions: {
input: {
app: './src/js/app.js'
},
output: {
sourcemap: true
}
}
},
plugins: [
nodeResolve({
moduleDirectories: [path.resolve('./node_modules')]
}),
ViteRestart({
reload: ['./templates/**/*']
}),
vue()
],
publicDir: './src/public',
resolve: {
alias: {
'@': '/src'
}
},
server: {
+ host: '0.0.0.0',
+ hmr: {
+ host: 'liamrella.test', // you could make this an ENV var
+ port: '3000',
+ path: '/'
+ }
}
});
<?php
use craft\helpers\App;
return [
'useDevServer' => App::env('DEV_MODE'),
'manifestPath' => '@webroot/dist/manifest.json',
'devServerPublic' => App::env('VITE_DEV_SERVER_PUBLIC'),
'serverPublic' => App::env('SITE_URL') . '/dist/',
'errorEntry' => '/src/js/app.js',
'cacheKeySuffix' => '',
- 'devServerInternal' => App::env('VITE_DEV_SERVER_PUBLIC'),
+ 'devServerInternal' => 'http://localhost:3000/',
'checkDevServer' => true,
];
DEV_MODE=1
SITE_URL=http://liamrella.test
- VITE_DEV_SERVER_PUBLIC=http://localhost:3000/
+ VITE_DEV_SERVER_PUBLIC=http://liamrella.test:3000/
WEB_ROOT_PATH=/app/web (aliased as @webroot) Edit: This assumes you're running vite from within the Nitro container. |
@andrewmenich nice! And this is running Vite inside of Nitro yes? |
Yep! I just updated my original post to clarify. |
Fantastic. I'm praying you decide to write a blog on this :) |
I plan to! I've pinged a few folks on Discord with my findings to see if it works for them. Just trying to avoid the "but it works on my machine!" situation. |
@andrewmenich I just tested it and it works! Thanks heaps! |
Unfortunately for my setup, despite following what's been said here, is still not working. The only way I could get rid of the CORS error was to put the Vite Server on a subdomain, like so (Otherwise the requests responded with a 502 ERROR):
This solves the CORS issue, but my requests to Edit: Correction, doesn't seem to solve the CORS issue, just the 502 ERROR when trying to load the script directly. And just to confirm the 502 ERROR, ran from the host machine:
Any help would be much appreciated. Update So, the only way I have been able to get this to work, on top of what's suggested above, is to make sure of the following (it could be all, or a combination):
The reason I had set Nitro's config path for the site to be the If anyone can make the |
Thanks @andrewmenich for this, works a treat here when using http. Just wondering if anyone has managed to get it all going on https? I've tried the vite-plugin-mkcert however on 'npm run dev' within nitro, mkcert fails with Permission Denied, I suspect it doesn't work well with the Alpine distro. |
I have Vite working as expected in Nitro 2 when running Now I've installed the Critical CSS companion package https://github.com/nystudio107/rollup-plugin-critical and have it configured as follows in critical({
criticalUrl: 'http://craft-starter.nitro:8080/',
criticalBase: './web/dist/criticalcss',
criticalPages: [
{ uri: 'contact', template: 'contact' },
],
criticalConfig: {
},
}), When I run
Here's some info on running Puppeteer on Alpine: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-on-alpine Is there something I can do/need to change, or is it Nitro itself? Thanks! |
I've been playing around with trying to get this working with https, since it seems that mixed content errors are the only thing holding me back from a working dev server. it seems that if you have certs, you can just point to them from your
But we know that nitro already generates certs for the sites it serves ....... so I'm curious if anyone knows where those are stored / if we could just point to them from our vite config rather than using vite-plugin-mkcert which seems to have permissions issues? |
Hi, we are closing this issue as we have decided to retire Nitro, so no additional work will occur on this project. You can read the official blog post here https://craftcms.com/blog/retiring-craft-nitro. We appreciate everyones feedback and involvement and we look forward to refocusing our efforts on Cloud! If you're looking for a new local development environment, we recommend DDEV and have a knowledge base article to help you with the transition: https://craftcms.com/knowledge-base/migrating-from-craft-nitro-to-ddev. |
Description
Working through Andrew Welsh's article about using Vite for local dev with Craft. I feel like this config is very close to working but have run into a CORS issue, but can't seem to figure out why.
There is a heap of information here, but please let me know if I have missed anything that you might need to reproduce.
Current config
vite.config.js
vite.php
package.json
relevent env vars
Steps to reproduce
Additional info
The text was updated successfully, but these errors were encountered: