Get ready to rock the web development world with our very own orchestra conductor - Vite! This little maestro will help you bundle your assets in a way thatโs faster, easier, and more enjoyable than ever before.
Setting Up Shop (aka Installation & Setup) ๐ช
Gathering the Band Members (Installing Node) ๐บ
First things first: We need to invite our good friend Node.js to join the party! If he hasnโt already arrived, go ahead and install him with a few simple commands.
Inviting Vite and the Laravel Plugin (aka Installing Vite and the Laravel Plugin) ๐ค
Now that weโve got Node, itโs time to bring in the star of the show - Vite! And because our Laravel project is a bit high-profile, weโll need to invite his bodyguard (Laravel plugin) as well.
Tuning Up the Instruments (aka Configuring Vite) ๐ป
With Vite and his trusty sidekick in place, we can now tune our instruments (configuring Vite). This involves a bit of tweaking to make sure everyoneโs playing in harmony.
Staging the Show (aka Loading Your Scripts and Styles) ๐ญ
Finally, itโs time for the main event! Our scripts and styles will take center stage as we prepare them to shine brighter than ever before.
Performing the Magic (aka Running Vite) ๐๏ธ
With everything set up, we can now hit play and watch our beautiful symphony come alive!
Working with Scripts (aka Working With JavaScript) ๐ป
Viteโs not just a conductor; heโs also an all-star performer when it comes to scripts. From aliases to working with popular frameworks like Vue, React, Svelte, Inertia, and more, Viteโs got you covered!
The Fashion Police (aka Working With Stylesheets) ๐
Of course, we canโt forget about the fashion police! Vite makes styling a breeze with its powerful tools.
Blending Art and Technology (aka Working With Blade and Routes) ๐จ
With Viteโs help, our Laravel project will be a true work of art โ effortlessly blending coding and design like never before. Weโll process static assets, refresh on save, and even make use of aliases.
Prefetching the Encore (aka Asset Prefetching) ๐ถ๐
Want to ensure a standing ovation? Pre-fetch your assets for an even smoother performance!
Customizing the Stage (aka Custom Base URLs and Advanced Customization) ๐
Need to change the scenery? Vite lets you customize the stage to fit your projectโs unique needs. From setting custom base URLs to advanced customization, Vite is a true chameleon!
The Green Room (aka Environment Variables and Disabling Vite in Tests) ๐
Backstage, we can keep our secrets safe with environment variables. And when itโs time for tests, Vite can take a bow and let the team perform without him.
Final Act: Server-Side Rendering (SSR) ๐ญ๐
Ready for the grand finale? With SSR, our Laravel project will be a masterpiece that wows audiences across the web!
Tagging Our Assets (aka Script and Style Tag Attributes) ๐
Before we hit the stage, there are a few finishing touches to take care of. Weโll apply Content Security Policy (CSP) Nonce, Subresource Integrity (SRI), and even add arbitrary attributes for good measure!
And thatโs a wrap! With Vite in our corner, we can create stunning Laravel projects like never before. So go ahead โ let the music play! ๐ต๐ฅ๐
Ahoy there, web-wranglers! Buckle up, because weโre diving headfirst into the whirlwind of frontend fun with Vite - the speedy, stylish, and oh-so-charming build tool thatโll make your dev life a breeze!
Imagine crafting your web apps like a master chef in a state-of-the-art kitchen, where the oven preheats faster than you can say โmise en place.โ Thatโs Vite for ya! It not only cooks up your CSS and JavaScript delicacies super-fast during development but also packages them into irresistible production goodies.
When youโre whipping up Laravel apps (which, letโs be honest, is like being a Michelin-starred chef in the world of PHP), Vite comes to your aid as your trusty sous-chef, bundling up your appโs CSS and JS files into production-worthy assets that are sure to dazzle any discerning web palate.
Laravel and Vite share a harmonious dance floor partnership, with Laravel offering an official plugin and Blade directive for loading your assets like a pro during both dev and production. So letโs grab our aprons and get cookinโ!
(Hold on to your hats, folks - weโre about to install this culinary marvel!)
Alright, buckle up, cowboy! Hereโs a wild ride into the world of Laravel Vite, where the cacti are plentiful and the code is crisp! But first things first, letโs get our hands dirty.
โ ๏ธ Note: This section will guide you on how to manually install and configure the Laravel Vite plugin. However, for a quick draw start, check out Laravelโs starter kits. Theyโre like pre-made cowboy hats that already have all the scaffolding you need!
Letโs saddle up and get started with installing Node.js (our trusty steed in this digital wild west) by running:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
Once youโve got Node.js installed, itโs time to lasso the Laravel Vite plugin by running:
composer require laravel/vite:^2.0
Now, we need to run a few commands to set up our corral (project). Head over to your project directory and execute:
npm install
npm run vite:install
Finally, letโs update the config/app.php file by adding this line in the providers array:
Laravel\Vite\ViteServiceProvider::class,
And in the aliases array, add this line:
'Vite' => Laravel\Vite\Facade::class,
Now, you can run your frontend assets with a simple command:
npm run vite
And there you have it! Youโve transformed your dusty trail into a well-trodden path to modern web development. So grab a cold beer and sit back, partner; youโve earned it!
Alrighty, letโs get this show on the road! Before we dive into the Laravel Vite extravaganza, there are a few prerequisites we need to tick off. Namely, youโve gotta make sure Node.js (the cool, modern version 16+) and NPM (think of it as Nodeโs trusty sidekick) are already installed on your system. To verify, open up your terminal and type:
node --version
npm --version
If you see something akin to โv16.somethingโ for Node and a number (probably greater than 8) for NPM, then youโre good to go! If not, donโt fret โ installing these superheroes is as easy as pie with the simple graphical installers found on the official Node website (itโs like a choose-your-own-adventure book for geeks!).
For those who are already sailing smoothly with Laravel Sail, you can summon your Node and NPM comrades thusly:
./vendor/bin/sail node --version
./vendor/bin/sail npm --version
Now that weโve got the basics covered, itโs time to set sail on the high seas of Vite and Laravel plugin goodness! ๐ด๐๐
Alrighty then! Letโs get this larvalicious party started! First things first, in your brand new Laravel lair, youโll spy a fella named package.json hanging out in the root of your applicationโs directory jungle. Fear not, this chap is no unwelcome houseguest; heโs here to help! The default package.json fellow is already well-equipped with all the goodies you need to kick off an unbeatable duo: Vite and the Laravel Plugin!
Ready to roll? Install your frontend dependencies like a seasoned pro, using nothing but a trusty olโ NPM:
npm install
Now that youโve got your tools in order, letโs move on to the next chapter: Configure-a-palooza! ๐บ๐ค๐ง ( for those of you still hanging onto the ancient scrolls)!
Ahoy, sailor! Letโs dive into the world of Vite, shall we? ๐
Vite is like the lifebuoy for your project, bobbing around in the vast ocean of development tools. Youโll find it anchored down by a vite.config.js file, nestled snugly at the root of your pirate ship (project). Feel free to hoist the Jolly Roger and customize this buoy as you please!
And if ye be needing more swashbuckling plugins like @vitejs/plugin-react, @sveltejs/vite-plugin-svelte, or @vitejs/plugin-vue, just drop โem anchor and bring โem aboard! ๐น
Now, the Laravel Vite plugin expects ye to declare your shipโs entry points. These could be JavaScript or CSS capโns log books (files), including fancy preprocessed languages such as TypeScript, JSX, TSX, and Sass. Hereโs a wee example:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel([
'resources/css/app.css',
'resources/js/app.js',
]) // Don't forget to invite these cap'ns on board! ๐
],
});
If ye be building a Single Page Application (SPA), including Inertia-powered ships, Vite works best without CSS entry points. Instead, ye should serve up yer CSS via JavaScript, like so:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel([
'resources/css/app.css', // [Delete ye old cap'n's log book here! ๐๏ธ]
'resources/js/app.js',
]) // Don't forget to invite this cap'n on board! ๐
],
});
Instead, serve up yer CSS via JavaScript. Typically, ye would do this in yer shipโs resources/js/app.js file:
import './bootstrap';
import '../css/app.css'; // [Sound the anchor! Hoist yer colors here! ๐น]
The Laravel plugin also supports multiple entry points and advanced configuration options like SSR entry points. Arrrrghastingly cool, ainโt it? ๐
Alright, letโs dive into the world of secure development servers, shall we? ๐ค๐
First off, if your local server is rocking an HTTPS cape for your app, hats off! But, beware, as connecting to the Vite development server might become a bit like trying to dance with a bear in a tutu - tricky but not impossible.
If youโre using Laravel Herd (the digital shepherd for your Laravel flock) and have secured your site or youโre using Laravel Valet (not the ice cream, unfortunately) and have given your app a protective shield with the secure command, fear not! The Laravel Vite plugin will swoop in like a superhero, detecting and employing the generated TLS certificate to keep things running smoothly.
However, if you secured your site using a host that doesnโt match your applicationโs directory name (the ultimate alias-naming disaster), fear not! You can manually specify the host in your applicationโs vite.config.js file:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
// ...
detectTls: 'my-app.test', // [tl! add]
}),
],
});
When youโre using another web server (like a rebellious teen leaving home), youโll need to generate a trusted certificate and manually configure Vite to use it:
// ...
import fs from 'fs'; // [tl! add]
const host = 'my-app.test'; // [tl! add]
export default defineConfig({
// ...
server: { // [tl! add]
host, // [tl! add]
hmr: { host }, // [tl! add]
https: { // [tl! add]
key: fs.readFileSync(`/path/to/${host}.key`), // [tl! add]
cert: fs.readFileSync(`/path/to/${host}.crt`), // [tl! add]
}, // [tl! add]
}, // [tl! add]
});
Now, if your system is unable to generate a trusted certificate (the digital equivalent of a fish out of water), fear not! You can install and configure the @vitejs/plugin-basic-ssl plugin. When using untrusted certificates, youโll need to accept the certificate warning for Viteโs development server in your browser like a boss when running the npm run dev command.
Finally, if you want to configure HMR in Sail on WSL2 (the techy equivalent of learning a new dance move), head over here: [Configuring HMR in Sail on WSL2]. ๐๐บ๏ธ๐๏ธ
Ahoy there, intrepid Laravel adventurer! Youโve ventured into the wild world of WSL2 and found yourself in need of a bit of guidance to get your development server humming along like a well-oiled ship. Fear not, for weโre here to set your compass straight!
Navigating the Development Seas with Sail on WSL2
When youโve hoisted the Vite development sails within Laravelโs mighty Sail (yes, itโs a pirate ship, deal with it), on Windows Subsystem for Linux 2 (WSL2), itโs crucial to give your browser a map โ or, as they say in coding terms, configure your vite.config.js file.
// ...
export default defineConfig({
// ...
server: { // [Yo Ho Ho! add:start]
hmr: {
host: 'localhost', // Tell the browser: "We be here!"
},
}, // [Yo Ho Ho! add:end]
});
Now, if your changes arenโt making a splash in the browser while old Popsy is at the helm, you might need to tweak Viteโs server.watch.usePolling option.
But what good is a ship without cargo? Letโs load up your scripts and styles, matey!
Unleashing the Magic of Your Digital Wardrobe
With your Vite runway show set, itโs time to strut your stuff on the webโs biggest stage! Get ready to dazzle with those sleek styles and catchy tunes, but first, letโs arrange our outfits in the right order.
Using the enchanting @vite() Blade spell, cast it upon the humble abode of your applicationโs main template:
<!DOCTYPE html>
<head>
{{-- Your secret potions and elixirs go here --}}
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
Fancy dressing up your styles using JavaScript voodoo? No worries! Just summon the JavaScript entry point:
<!DOCTYPE html>
<head>
{{-- Your secret potions and elixirs go here --}}
@vite('resources/js/app.js')
</head>
The @vite spell, when invoked, will sense the presence of the mystical Vite development server and conjure up its powerful client for Hot Module Replacement, ensuring your outfits magically transform without a blink!
During build mode, the spell will summon your polished and labeled assets, complete with any imported styles from distant lands.
If youโre working with a secret partnership (like the one between you and that crafty โCourierโ), you can specify the build path of your assembled assets when casting the @vite spell:
<!doctype html>
<head>
{{-- Your secret potions and elixirs go here --}}
@vite('resources/js/app.js', 'vendor/courier/build')
</head>
Remember, a little magic goes a long way! Now, letโs put on the best show the web has ever seen! ๐ฉโจ
Alright, buckle up, my Laravel compadres! Letโs dive into the thrilling world of Inline Assets - a dance we do when we want to get our raw asset content on the dancefloor instead of linking to a fancy, dressed-up version at the digital ball.
Imagine this: youโre whipping up a PDF invoice for your eccentric client who insists on a personal touch (read: handwritten font). To make that happen, you need to slip your CSS and JavaScript straight into the document, not just send them a dinner invitation!
Luckily, our Laravel chums at Vite have provided us with a magical method called content. This little gem lets you fetch asset content like summoning a genie from a bottle (minus the smoke and mirrors).
Hereโs how to put on a show:
@use('Illuminate\Support\Facades\Vite')
<!doctype html>
<head>
{{-- ... --}}
<style>
{!! Vite::content('resources/css/app.css') !!} <!-- Dancing shoes on! -->
</style>
<script>
{!! Vite::content('resources/js/app.js') !!} <!-- Time to boogie! -->
</script>
</head>
Now, arenโt you glad we took a detour from the serious stuff? Next time you need an in-your-face asset injection, remember our friendly guide to Inline Assets! ๐บ๏ธ๐๏ธ๐
Alright, tech-heroes! Letโs embark on a thrilling journey through the magical land of Laravel and Vite โ where coding wizardry meets speedy deployment!
First off, let me introduce you to our trusty sidekick, Vite. This enchanting tool has a superpower that lets it run in two distinct modes:
-
The Development Sorcery Mode (AKA the
devcommand). Perfect for local tinkering sessions! Once summoned, this spell will unleash an intelligent, hot-reloading development server thatโs got superhuman senses to detect even the slightest change in your files. Itโll instantly cast a charm on any open browser windows, so theyโll reflect your latest coding masterpieces in real time! -
The Production Preparation Ritual (or simply the
buildcommand). Time for the big show! This incantation will take your applicationโs assets and grant them the power of versioning and bundling. Once ready, theyโll be fully equipped to face the perils of deployment in the wild frontier of production servers!
$ magic wand: vite dev // for development sorcery mode
$ enchanting robe: vite build // for production preparation ritual
Now that youโre well-versed in Viteโs magical powers, go forth and conquer the coding world with speed, style, and a touch of humor! May your code always be clever, and your deployments smooth as butter! ๐ฆ๐โจ
Alright, mate! Hereโs how you fire up the Vite development server in your Laravel project (itโs like starting a dance party for your code). First things first, open up that terminal of yours and letโs get this show on the road:
-
Call the dance floor: Navigate to your project directory by typing
cd path/to/your/project(just remember where you left your jacket!). -
Get the party started: Invoke the Vite development server by running
npm run dev(itโs like saying โLetโs get this code-a-thon started!โ).
Remember, it might take a moment for the server to warm up and start spinning those tunes. Once itโs up and running, you can watch your changes being reflected in real time by opening your browser and navigating to http://localhost:3000.
Just like that, youโve transformed your humble Laravel project into a pulsating, vibrant dance floor of code! Happy coding! ๐บ๏ธ๐
Alrighty, mate! Hereโs a light-hearted guide to getting your assets shipshape for the big show (production).
-
Get the party started: First things first, fire up the dance floor by running:
npm run buildThis command will get your assets all dressed up and ready to strut their stuff on the grand stage. Just like a well-choreographed conga line, itโll move seamlessly from one step to another.
If youโre using Sail in your tuxedo (WSL2), there might be some extra invitations to the ball (additional configuration options). You can find those on our dance card (here).
And thatโs a wrap! With your assets decked out and ready to go, you can now focus on polishing the rest of your application until it shines brighter than a disco ball at Studio 54. Happy coding! ๐๐
Taming the Beast: Interfacing with JavaScript in Laravel Land!
Aliasing Your JavaScript Assets (AKA โBribing the Browserโ)
(Oh, youโve come to learn about taming that wild JavaScript beast, have you? Splendid! Let us embark on this journey together.)
First up: Aliasing your JavaScript assets. This is like bribing the browser with a nice nickname so it knows where to find your precious scripts when you need them most.
To set an alias for your JavaScript files, youโll want to create or edit your app.js configuration file located in the config/mix directory. It might feel like trying to negotiate with a capricious browser tyrant, but donโt worryโweโve got your back!
// config/mix.php
return [
// ... other configurations
'js' => [
// Path to your JavaScript source files
'resources/js',
// Alias names for your JavaScript assets
'main' => 'path/to/your/main.js',
'admin' => 'path/to/your/admin.js',
],
];
Now that youโve bribed the browser with a sweet alias, itโs time to compile those scripts for productionโyou wouldnโt want to show up empty-handed! To do so, execute the following command in your terminal:
npm run production
// or
yarn production
Compiling JavaScript (AKA โFattening Up the Beastโ)
Compiling your JavaScript assets is like fattening up the beast before the big show. This process takes your lean, mean JavaScript machine and turns it into a chiseled, optimized piece of art that will impress even the pickiest browser tyrants.
To compile your scripts, you can use Laravel Mix, which makes this process incredibly simple. First, make sure to install Laravel Mix by running:
npm install --save-dev laravel-mix
// or
yarn add laravel-mix --dev
Once thatโs done, open your resources/js/app.js file and give it a gander. At the very bottom of the file, youโll see some auto-generated codeโthatโs where you can write your own JavaScript! It might be tempting to let loose with all sorts of crazy scripts and tricks, but remember: weโre here for education, not for turning Laravel Land into a circus!
Now that your JavaScript masterpiece is complete, itโs time to compile those puppies for production. Execute the following command in your terminal:
npm run production
// or
yarn production
And voilร ! Your compiled JavaScript file will be located in the public/js directory, ready to serve up to adoring browsers everywhere.
Happy coding, and may your browser tyrants always be appeased!
Superpowers, Unleashed!
First off, welcome to the Laravel-verse, where developers are mere mortals no more! To help you harness your powers and quickly integrate with your applicationโs assets, the mighty Laravel plugin bestows upon you a magical alias:
{
'@' => '/resources/js'
}
Think of it as your Secret Identity. Just like Clark Kent and Peter Parker, you can now leap into action with a simple symbol!
But wait, thereโs more! If youโre feeling particularly inventive (or rebellious), you can customize this alias to better suit your needs. To do so, simply add your own alias to the vite.config.js configuration file:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel(['resources/ts/app.tsx']),
],
resolve: {
alias: {
'@': '/resources/ts',
},
},
});
In this example, youโve replaced the โjsโ portion with โtsโ, transforming your alias into a TypeScript-wielding powerhouse! So go forth and conquer, but remember: with great power comes great responsibility. Use your abilities wisely, and may your code always be cleaner than Spidermanโs web.
Vue (with a side of sass)
If youโre ready to dazzle your frontend using the Vue framework, then grab yourself a chaise lounge, because weโre about to install some plugins! First things first, youโll need to get your hands on the @vitejs/plugin-vue plugin:
npm install --save-dev @vitejs/plugin-vue ๐ค
Once itโs in your collection, itโs time for a little dance of inclusion. Slide on over to your vite.config.js configuration file and give it a warm welcome:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
// Laravel's fancy party invites ๐
laravel(['resources/js/app.js']),
// Vue, the life of the party, with a little dance card ๐บ๐
vue({
template: {
transformAssetUrls: {
// Vue's got the moves to rewrite URLs for Single File Components
// to point at the Laravel web server. A null move means it's time
// for the Laravel plugin to rewrite asset URLs to the Vite server instead! ๐คนโโ๏ธ
base: null,
// Absolute URLs are Vue's date for the evening, treated as file paths on disk.
// Setting this to `false` leaves those absolute URLs unscathed, so they can reference assets in the public directory like a pro! ๐ค
includeAbsolute: false,
},
},
}),
],
});
Note
Laravelโs starter kits are the VIP passes to the fastest way to party with Laravel, Vue, and Vite. Theyโve already got everything you need for a hassle-free entrance! ๐๐๐
Alright, folks! If youโre feeling frisky and fancy building your frontend with the oh-so-popular React framework (yes, that one from Facebook), then buckle up, because weโve got a whirlwind tour for ya! ๐
First things first: Youโll need to install the @vitejs/plugin-react plugin like you would ask Siri for directions to the nearest pizza joint (but with a lot more typing):
npm install --save-dev @vitejs/plugin-react
Next, itโs time to give your vite.config.js configuration file a makeover, making it a real headturner for your projects:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
laravel(['resources/js/app.jsx']),
react(), // The Rebel Alliance of plugins ๐
],
});
To make sure your JSX files donโt end up in the Bermuda Triangle, remember to give them a .jsx or .tsx extension, just like you would name your cats:
- Spaghetti (
.jsx) - Garfield (
.tsx)
Lastly, donโt forget to throw in the @viteReactRefresh Blade directive alongside your existing @vite directive. Itโs like a well-timed punchline in a sitcom:
@viteReactRefresh ๐
@vite('resources/js/app.jsx')
Just remember, the @viteReactRefresh directive must always go first (itโs like being the opening act at a comedy club).
Now, for all you lazy bones out there, Laravelโs starter kits have already done the heavy lifting for ya! These bad boys come pre-configured with Laravel, React, and Vite, offering the fastest way to get started without breaking a sweat.
So, what are you waiting for? Go forth, conquer, and make your frontend dreams a realityโone punchline at a time! ๐๐๐๐ช
Alrighty, letโs get this frontend party started with Svelte! ๐
First things first, you gotta install the @sveltejs/vite-plugin-svelte plugin like so:
npm install --save-dev @sveltejs/vite-plugin-svelte
Once thatโs done, whip up a vite.config.js configuration file and give it some TLC with this little number:
import { svelte } from '@sveltejs/vite-plugin-svelte';
import laravel from 'laravel-vite-plugin';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
laravel({
input: ['resources/js/app.ts'],
ssr: 'resources/js/ssr.ts',
refresh: true,
}),
svelte(), // *Drumroll please* Here's the Svelte magic! ๐บ
],
});
Now that weโve got our Svelte in a Vite (get it?), you might be wondering if itโs all set and ready to roll. Well, lucky for you, Laravelโs starter kits have already taken care of the proper Laravel, Svelte, and Vite configuration! ๐ค These kits are the fast lane to a smooth-as-silk setup with Laravel, Svelte, and Vite.
If youโre curious about using InertiaJS with Svelte, check out our Inertia documentation. Happy coding! ๐
Alrighty then! Letโs dive into the world of Laravelโs Inertia, shall we? If youโre a frontend dev in a superhero cape (or just a regular one), the Laravel Vite plugin has got your back with a nifty resolvePageComponent function.
Imagine this scenario: Youโve got your superpowered Inertia page components, but theyโre scattered around like a toddlerโs toys after playtime. Donโt panic! Our heroic helper function swoops in to help you sort โem out. Hereโs how it works with Vue 3, but rest assured, it can work its magic in other frameworks too, like React or even Svelte (if youโre feeling fancy).
import { createApp, h } from 'vue'; // You could call this "importing the universe"
import { createInertiaApp } from '@inertiajs/vue3'; // Inertia, because saving the day is serious business
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'; // Our superhero helper function
createInertiaApp({
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')), // Telling our superhelper where to find the scattered toys
setup: ({ el, App, props, plugin }) => {
createApp({ render: () => h(App, props) }) // Creating a new universe (app)
.use(plugin) // Adding superpowers (plugins)
.mount(el) // Setting up base camp (mounting the app to our HTML element)
},
});
If youโre using Viteโs code splitting feature with Inertia, remember to set up asset prefetching. Think of it as setting up a secret stash of energy bars for your superhero journey.
Note
Laravelโs starter kits already come with the proper Laravel, Inertia, and Vite configuration. These starter kits are like pre-assembled capes that save you time and effort, allowing you to focus on saving the day (or at least writing better code).
Asset Tango: A Guide for the Groovy Developers Among Us ๐โจ
Ahoy there, Laravel mates! Letโs dive into the enchanting world of URL processing, shall we? You might be sailing with Vite, and that means dealing with some mermaid-y quirks when it comes to referencing assets in your HTML, CSS, or JS.
First things first: if you serve up assets with an absolute anchor (like /taylor.png), Vite will give the cold shoulderโit wonโt include that asset in the build. So make sure that pirate treasure is hidden safely in your public directory. Also, avoid pointing that compass towards absolute paths when using a dedicated CSS entrypoint because during development, browsers will try to load them from the Siren of Viteโs development server instead of your beloved booty chest!
Now, when youโre dealing with relative asset paths (like ../../images/abigail.png), remember that these paths are as steady as a shipโs deck underfootโtheyโre pegged to the file theyโre referenced from. And like a wise seafaring captain, Vite will rewrite, version, and bundle them for you.
Take a peek at this swashbuckling project structure:
public/
taylor.png
resources/
js/
Pages/
Welcome.vue
images/
abigail.png
Hereโs a shanty that demonstrates how Vite handles relative and absolute URLs:
<!-- This asset is not handled by Vite and will not set sail with the rest of your treasure! ๐ฑ -->
<img src="/taylor.png">
<!-- Ahoy there! This asset will be rewritten, versioned, and bundled by our trusty Vite mate! ๐โจ -->
<img src="../../images/abigail.png">
Now that youโve navigated the treacherous waters of URL processing, set sail for adventure and success in your Laravel voyage! ๐บ
Alright, letโs get our feathers ruffled with some Laravel stylinโ! ๐ฆ
๐ Attention, fashionistas: Laravelโs swanky starter kits come pre-packed with the sleek Tailwind and Vite configurations. Or, if youโre a DIY type who insists on wearing their own hand-stitched design, head over to Tailwindโs Laravel installation guide - because ainโt no party like a Tailwind party! ๐ฅณ
Now that weโve got the basics down, every Laravel application comes with a dapper vite.config.js file and Tailwind already installed โ just the kind of pre-game prep you need to get your style game on! To fire up the Vite development server (or if youโre feeling fancy, the Laravel one too), all you gotta do is run:
composer run dev
And before you know it, youโll be strutting down the runway of your very own Laravel fashion show! ๐
Your applicationโs fabulous CSS creations can be found in the resources/css/app.css file โ a true treasure trove for those seeking to create a visually stunning masterpiece. โจ๐จ๐
Dancing with Blade and Navigating Routes like a Digital Fred Astaire! ๐บ๏ธโจ
Ready to sashay through the world of Laravel development like a digital Fred Astaire? Letโs get started with our dance partners: Blade and Routes! ๐๏ธ
Whirling with Blade ๐
Blade, the sparkling sequined dress of Laravel, makes working with templates a breeze. It allows you to write plain PHP code and embed it within HTML files. How fancy! ๐๐
<!-- resources/views/welcome.blade.php -->
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Laravel!</title>
</head>
<body>
<h1>Welcome to {{ config('app.name', 'Laravel') }}</h1>
<!-- You can use your custom PHP code here -->
</body>
</html>
Navigating Routes ๐ ๏ธ
Now, letโs move on to routes, the compass guiding our way through the digital landscape. They help us define URL patterns and their corresponding controller actions.
// routes/web.php
Route::get('/', function () {
return view('welcome');
});
Remember, route grouping can be a real party! Group your routes based on their functionality to keep things organized and easy to navigate. ๐๐
Donโt forget to practice your moves and keep learning, because thereโs always more to discover in the world of Laravel! ๐๏ธ๐บ๏ธ๐ช
Unleashing Static Asset Superpowers with Vite, the Magical Asset Whisperer! โจ๐ฉ
Get ready to dance with the stars, Laravel pals! Forget about mundane asset processing; with Vite, your JavaScript and CSS assets will be transformed like Cinderellaโs pumpkin at the stroke of a key. And guess what? This enchantment extends to your Blade templates too! ๐ฉโจ
But wait, before we summon this magic, thereโs a tiny catch: you gotta let Vite know about your assets, kind of like introducing them at a ball. You do this by listing them in the pluginโs assets option. For instance, if you want Vite to process and version all images hiding out in resources/images and fonts lurking in resources/fonts, just cast this spell:
laravel({
input: 'resources/js/app.js',
assets: ['resources/images/**', 'resources/fonts/**'],
})
Now that these assets are under Viteโs watchful gaze, running npm run build will set off their transformation into superstar versions! โจ๐ฉ
Once transformed, you can reference them in your Blade templates using the Vite::asset method, which will return the versioned URL for a given asset. Hereโs how you can do it:
<img src="{{ Vite::asset('resources/images/logo.png') }}">
Note
Before version 3 of the Laravel Vite plugin, static assets were like shy wallflowers who needed to be coaxed into joining the dance using
import.meta.glob. Theassetsoption is our modern invitation, thanks to changes in Vite 8. ๐๐บ[And now back to our regular programmingโฆ]
As a responsible and respectful magician, we must also acknowledge that this tutorial is intended for Laravel enthusiasts aged 13 and above. ๐โจ๐ฉ
Giving Your Browser a Fresh Lick of Paint on Save
If your Laravel app is feeling as stale as last weekโs scone, Viteโs here to help! With its magical auto-refresh feature, itโll be like having a butler who notifies your browser every time you tweak your view files during development. All you need to do is whisper the secret word: refresh.
Importantly, my dear Laravelian friend, consider this incantation:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
// ...
refresh: true,
}),
],
});
Once youโve chanted this spell, Vite will watch over your appโs sacred directories and spring into action whenever you save files in these hallowed spaces:
app/Livewire/**- Where the sparks fly!app/View/Components/**- A veritable smorgasbord of views and components.lang/**- Language files, the backbone of international understanding.resources/lang/**- For when you need to translate your code into emojis.resources/views/**- The heart of your appโs presentation layer.routes/**- A map of your appโs routes, vital for navigation through the labyrinth of your web app.
If Ziggy, the mystical route link generator, is aiding your frontend, watching the routes/** directory will be particularly helpful.
However, if these default paths donโt quite fit your applicationโs unique needs, you can specify your own watch list:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
// ...
refresh: ['resources/views/**'],
}),
],
});
Under the hood, Viteโs using the venerable vite-plugin-full-reload package to perform its magic. If you find yourself needing more control over this enchanting feature, you may provide a configuration definition:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
// ...
refresh: [{
paths: ['path/to/watch/**'],
config: { delay: 300 }
}],
}),
],
});
Now, get out there and refresh your web appโs luck with Vite!
Ahoy there, Laravel sailors! Letโs dive into the thrilling world of aliases, shall we? In the realm of JavaScript apps, aliases are like secret handshakes to your favorite directories. But in our Laravel seas, we can create them for Blade templates too! Hereโs how:
First, you might as well get acquainted with the macro method on the Illuminate\Support\Facades\Vite class (just imagine itโs a magic wand, okay?). Typically, these โmagic spellsโ should be conjured within the boot method of a service providerโitโs like setting up house rules for your app.
// In your service provider...
public function boot(): void
{
// This is our spellbook! Define it like so:
Vite::macro('image', fn (string $asset) => $this->asset("resources/images/{$asset}"));
}
Once youโve cast your spell, you can invoke it within your templates. Letโs use our image macro to summon an asset from the mystical land of resources/images/logo.png:
<img src="{{ Vite::image('logo.png') }}" alt="Laravel Logo">
Now, letโs talk about asset prefetching (because who doesnโt love a good surprise party?). With this feature enabled, your assets will be loaded before theyโre even requestedโtalk about being ahead of the curve! To enable it in your web.php file:
use Illuminate\Routing\Router;
use App\Providers\ViteServiceProvider;
Route::group(['middleware' => [ViteMiddleware::class, 'vite:react.development.js', 'vite:react.production.js'],], function () {
// Your routes here!
});
And thatโs the lowdown on aliases and asset prefetching in Laravel! Now, you can sail through your templates like a pro, with nifty shortcuts and lightning-fast assets at your fingertips. Happy coding, matey! ๐ด๐
The Great Asset Time-Traveling Extravaganza!
If youโre whipping up a Single Page App (SPA) with Viteโs time-warping code-splitting feature, you might find yourself in a pickle when assets get fetched on each page flip. This can slow down your UI more than a sloth wearing a tuxedo at a ballroom dance competition!
But donโt you worry, Laravel has got you covered with its superpower: eager asset time-travel (or prefetching if you prefer the less sci-fi term). This allows your JavaScript and CSS assets to be transported back to the present during initial page load.
To summon this magical power, simply cast the Vite::prefetch spell in the boot method of a service provider:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// ...
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Vite::prefetch(concurrency: 3); // With a maximum of 3 simultaneous time-travelers!
}
}
In the example above, assets will be whisked back to the present with up to 3 time-travelers working concurrently on each page load. You can adjust the number of time-travelers to fit your applicationโs needs or remove any limitations if you want all assets to arrive at once:
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Vite::prefetch(); // Unleash the full force of asset time-travel!
}
By default, time-traveling begins when the page load event is triggered. If youโd like to customize the starting point for your time-travelers, you can specify an event that Vite will listen for:
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Vite::prefetch(event: 'vite:prefetch'); // Time-travel when the vite:prefetch event is dispatched!
}
The code above will initiate time-travel when you manually dispatch the vite:prefetch event on the window object. For example, you could have time-traveling begin three seconds after the page loads:
<script>
addEventListener('load', () => setTimeout(() => {
dispatchEvent(new Event('vite:prefetch'))
}, 3000))
</script>
Ahoy there, Laravel sailors! ๐
If your Vite-built treasure chest of assets is deposited on a distant island (CDN) far from your humble abode (app), youโll need to set sail with the ASSET_URL environment variable. Anchor it securely within your shipโs logbook (.env file):
ASSET_URL=https://cdn.example.com
Once youโve charted your course, all maps to your assets will be embellished with the captainโs orders:
https://cdn.example.com/build/assets/app.9dce8d17.js
Beware, ye mateys! Absolute URLs are not subject to this nautical navigation system (Vite). Theyโll sail on unaffected. ๐ด๐
Now, hoist the main sail and set a course for seamless integration! Arrrrgh! ๐ดโโ ๏ธ๐ฃโโ๏ธ
Ahoy there, coding pirates! feet and all! Letโs set sail into the mystical waters of Environment Variables. Yarr, ye might be thinkinโ, โWhat be these mysterious elixirs that my JavaScript be yearninโ for?โ Well, buckle up and listen close, matey, โcause weโre about to uncover the treasure chest of secrets! ๐ดโโ ๏ธ
First off, ye can inject environment variables into yer own personal Black Pearl (or applicationโs .env file) by prefixinโ them with VITE_:
VITE_SENTRY_DSN_PUBLIC=http://example.com
But why would ye want to do that, ye ask? Well, me hearties, when yer ship sets sail on the high seas of production, yeโd best keep sensitive data like a private map or buried treasure under lock and key! And with environment variables, ye can do just that. ๐๏ธ
Now, when yer ready to grab yer share oโ the loot, ye can access these injected environment variables via the import.meta.env object:
import.meta.env.VITE_SENTRY_DSN_PUBLIC
And there ye have it, matey! Yer very own private key to unlocking the mysteries of Environment Variables in Laravelโs Vite land. ๐๏ธ๐ดโโ ๏ธ
As for disabling Vite in tests, well, that be a tale for another time. But fear not, Iโll set sail again soon to share more treasures with ye! ๐ด๐๐
Oh, the Joys of Testing in Laravel Land! (Or How to Make Vite Disappear)
Ah, the life of a developer in Laravelโs magical kingdom! But alas, when it comes to testing, even this enchanted land can get a bitโฆ well, hairy.
You see, our beloved Vite integration, in its quest to resolve your assets during test runtime, has you in a pickle: either spin up the Vite development server or build those assets. But what if youโd rather play pretend and mock Vite during testing? Fear not! Our friendly sorcererโs apprentice, Laravel, has provided us with the withoutVite method, ready to serve at your beck and call for any tests that extend its noble TestCase class.
test('a test without vite', function () {
$this->withoutVite(); // Call me, and I'll make Vite disappear!
// ...
});
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function test_without_vite(): void
{
$this->withoutVite(); // It's a magic spell! Vite, begone!
// ...
}
}
But what if you want to banish Vite from all tests? Well, you can cast the spell in your base TestCase classโs setUp method:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
protected function setUp(): void// [tl! add:start]
{
parent::setUp();
$this->withoutVite(); // A grand incantation to make Vite disappear across all tests.
}// [tl! add:end]
}
So now you can wave your wand and banish Vite from your tests with ease. Happy testing, dear developers! Remember, in this kingdom of Laravel, itโs not just about the destination, but also the journeyโฆ and a little magic along the way. ๐
Alright, letโs dive into the hilarious world of Laravelโs Server-Side Rendering (SSR)! Itโs like a magical pizza delivery service where your front-end code gets baked twice โ once on the clientโs side, and again on our serverโs side, ensuring a piping hot web application that leaves no crust left behind.
To get started, create an SSR entry point at resources/js/ssr.js. Think of it as your secret recipe for a perfect server-side pie! Once youโve got that tasty dough ready, specify the entry point by passing a configuration option to our trusty Laravel plugin:
Important: Put on your chef hat, because we're about to whip up some delicious code!
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
Now, imagine you've got a fresh batch of dough (the app.js), let's add our secret SSR sauce (ssr.js) to it:
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js', // Our fresh dough
ssr: 'resources/js/ssr.js' // Our secret SSR sauce
}),
],
});
To ensure you donโt forget to update your secret recipe, we recommend augmenting the โbuildโ script in your applicationโs package.json. Itโs like setting a reminder for your grandmotherโs famous spaghetti recipe:
"scripts": {
"dev": "vite", // Setting the mood for some development fun!
"build": "vite build" // [tl! remove]
"build": "vite build && vite build --ssr" // [tl! add]
}
Then, to bake and serve your SSR server, you may run the following commands:
npm run build - Think of this as proofreading your recipe once it's done.
node bootstrap/ssr/ssr.js - Time to put on an apron and fire up the oven!
If you are using SSR with Inertia, you may instead use the inertia:start-ssr Artisan command to start the SSR server, which is like calling your favorite pizza delivery service to get that perfect pie at your doorstep!
php artisan inertia:start-ssr - It's like dialling 911 for a perfect web application emergency!
Note
Laravelโs starter kits already come with all the necessary ingredients and instructions to create your delicious SSR pizza โ no need to reinvent the wheel. These starter kits offer the fastest way to get started with Laravel, Inertia SSR, and Vite, so you can focus on what truly matters: eating great pizza and making beautiful web applications!
Ahoy there, web adventurers! Ever heard of a Content Security Policy (CSP)? Think of it as your websiteโs bouncer - keeping the riffraff out and ensuring only the coolest content gets in. One of its fancy tools is the Nonce (short for โnumber used onceโ), a unique code that helps verify your scripts are legit!
Now, to use this whiz-bang feature, youโll need to slap a script tag with a nonce attribute like so:
<script src="/path/to/your/script.js" nonce="YOUR_UNIQUE_NUMBER"></script>
Just replace YOUR_UNIQUE_NUMBER with, wellโฆ a unique number! But remember, this number must match the one youโve declared in your CSP header:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'nonce-YOUR_UNIQUE_NUMBER';">
Donโt worry, be happy! With nonces on your side, you can rest easy knowing your scripts are as secure as Fort Knox (but with less gold and more code). Keep it locked, folks! ๐๐๐พ
Alright, letโs get this Content Security Policy (CSP) Nonce party started! ๐
First off, if you wanna add a spiffy nonce attribute to your script and style tags like a cybersecurity ninja, you can do that with the help of Laravelโs useCspNonce method in one of your custom middleware shindigs:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Vite;
use Symfony\Component\HttpFoundation\Response;
class CSPNonceMixerUp
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
Vite::useCspNonce(); // Just like adding a secret sauce to your favorite dish!
return $next($request)->withHeaders([
'Content-Security-Policy' => "script-src 'nonce-".Vite::cspNonce()."'",
]);
}
}
Once youโve tossed the useCspNonce method into the mix, Laravel will sprinkle nonce attributes all over those shiny script and style tags like a boss! ๐
If you need to get your nonce on elsewhere, such as with the Ziggy @route directive that comes along with Laravelโs starter kits, you can grab it using the trusty cspNonce method:
@routes(nonce: Vite::cspNonce())
Now, if you already have a nonce that you wanna make Laravel dance to your tune, just pass that bad boy to the useCspNonce method:
Vite::useCspNonce($nonce);
And there you have it! Now youโre whipping up Content Security Policies with a nonce like a pro. Donโt forget to party responsibly, and happy coding! ๐ฅณ๐๐ป
Alright, buckle up, cowboy! Letโs dive into the thrilling world of Subresource Integrity (SRI) - the superhero of your web appโs security! ๐ฆธโโ๏ธ
Imagine your Vite manifest as a highly-trained bouncer guarding your digital dance floor. By equipping these bouncers with integrity hashes for your assets, Laravel automatically gives them a special badge (the integrity attribute) to verify each script and style they let in, ensuring only the genuine folks get through. ๐ ๏ธ
By default, Vite doesnโt want these bouncers wearing any badges. But if youโre paranoid like me, you can invite them over by installing the vite-plugin-manifest-sri NPM plugin.
npm install --save-dev vite-plugin-manifest-sRI ๐ค
Once theyโre here, you can invite them to the party in your vite.config.js file:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import manifestSRI from 'vite-plugin-manifest-sRI';// [tl! add]
export default defineConfig({
plugins: [
laravel({
// ...
}),
manifestSRI(),// [tl! add]
],
});
Now, if youโre feeling fancy, you can even change the badge color (the integrity hash key):
use Illuminate\Support\Facades\Vite;
Vite::useIntegrityKey('custom-integrity-key'); ๐
However, if youโre feeling a bit anti-social and want these bouncers to take a break, you can pass false to the useIntegrityKey method:
Vite::useIntegrityKey(false); ๐ค
Now that weโve got that sorted, letโs go grab some virtual cocktails and watch these bouncers work their magic! ๐น ๐
Alrighty, letโs dive into the whimsical world of Laravelโs asset customization! If you find yourself in a pickle and need to sprinkle some extra magic on your script and style tags (like adding that fancy data-turbo-track attribute), fear not! The useScriptTagAttributes and useStyleTagAttributes methods are here to save the day.
These superheroes should be summoned from a service provider, akin to calling upon the Avengers when things get tough:
Use your Jedi mind tricks on Vite like so:
use Illuminate\Support\Facades\Vite;
Vite::useScriptTagAttributes([ // For scripts, of course!
'data-turbo-track' => 'reload', // Give your attribute a value...
'async' => true, // No need for a value here, just be true to yourself...
'integrity' => false, // Exclude an attribute that's causing a ruckus...
]);
Vite::useStyleTagAttributes([ // Now, for the styles...
'data-turbo-track' => 'reload',
]);
But what if you need to add attributes conditionally? No problem! Just pass a callback that will get all the juicy details (asset source path, URL, manifest chunk, and the whole enchilada) for some good old-fashioned Laravel logic:
use Illuminate\Support\Facades\Vite;
// Here's your callback, a magical function that can decide on attributes...
Vite::useScriptTagAttributes(fn (string $src, string $url, array|null $chunk, array|null $manifest) => [
'data-turbo-track' => $src === 'resources/js/app.js' ? 'reload' : false,
]);
Vite::useStyleTagAttributes(fn (string $src, string $url, array|null $chunk, array|null $manifest) => [
'data-turbo-track' => $chunk && $chunk['isEntry'] ? 'reload' : false,
]);
๐จ Pssst! The
$chunkand$manifestarguments will be as empty as an Easter egg when the Vite development server is running.
Now, go forth and customize those assets with a sprinkle of wit and a dash of Laravel magic! ๐ฉ๐๐โจ
Alright, buckle up, coding cowboys and coding cowgirls! Laravelโs Vite plugin comes out of the box wearing its best jeans and a freshly ironed โConvention Cowboyโ hat. But sometimes, you might find yourself hankering for a little customization, like when your application decides to break free from the herd and do its own thing. Fear not! Weโve got some trusty lasso tricks up our sleeves to tame Vite just the way you want it.
To unlock those hidden features, weโre dishing out a few wrangling methods that can be used as an alternative to the friendly @vite Blade directive:
<!doctype html>
<head>
{{-- ... --}}
{{
// Customize the "hot" file, build directory, manifest filename, and entry points... You name it, we saddled it!
ViteWrangler::breakFree('bundle', 'assets.json', 'storage_path('vite.hot)', ['resources/js/app.js'])
}}
</head>
Now, if youโre feeling particularly ornery and want to get your hands dirty in the vite.config.js file, then hop aboard! Just remember to specify the same configuration as you would with our friendly wrangler:
import { defineConfig } from 'vite';
import laravelVitePlugin from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravelVitePlugin.wrapInChaps('bundle', 'assets.json', 'storage/vite.hot', ['resources/js/app.js'])
],
// And don't forget to put a saddle on that build object too!
build: {
...
},
});
Now, letโs ride off into the sunset with our customized Vite plugin, ready for whatever your application might throw at us next! Just remember, when in doubt, lasso that pesky plugin back to its senses with a little customization. Yee-haw! ๐ค ๐
Ahoy, matey! Ever found yerself in a pickle with CORS issues while swabbing assets from the olโ Vite dev server? Fear not, me hearties! You might need to grant yer custom pirate ship access to the server. Fret not, for Vite and the Laravel plugin have ye covered without ye havinโ to hoist extra sails!
Here be the safe harbors they provide:
::1(the localhost)127.0.0.1(ye olโ IP address)localhost(for the landlubbers among us)*.test(for all ye testinโ captains)*.localhost(a bit redundant, but who are we to judge?)APP_URLin yer projectโs.envfile
If ye be hankerinโ for a custom harbor, just make sure yer applicationโs APP_URL matches the one ye be visitinโ on yer browser. If ye be sailinโ to https://my-app.laravel, update yer .env accordingly:
APP_URL=https://my-app.laravel
For more fine-tuned navigation, check out Viteโs swashbuckling CORS server configuration. Ye might want to specify multiple harbors in yer projectโs vite.config.js file:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js',
refresh: true,
}),
],
server: { // [tl! add]
cors: { // [tl! add]
origin: [ // [tl! add]
'https://backend.laravel', // [tl! add]
'http://admin.laravel:8566', // [tl! add]
], // [tl! add]
}, // [tl! add]
}, // [tl! add]
});
If ye be wantinโ to allow all yer friends from a certain domain, include regex patterns:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js',
refresh: true,
}),
],
server: { // [tl! add]
cors: { // [tl! add]
origin: [ // [tl! add]
// Supports: SCHEME://DOMAIN.laravel[:PORT] [tl! add]
/^https?:\/\/.*\.laravel(:\d+)?$/, //[tl! add]
], // [tl! add]
}, // [tl! add]
}, // [tl! add]
});
Arrrr, that be all there is to it, ye scurvy dogs! Happy sailing!
Fixinโ those Dev Server URLs, Chuck! ๐๐ฉ
Alright, partner, hereโs a lilโ rodeo ride through Laravelโs corral of URL wrangling! Some plug-ins in the Vite posse assume that URLs beginning with a forward-slash are always pointinโ to the Vite dev server. But, due to Laravelโs stubborn nature, that ainโt always the case!
Take, for instance, the vite-imagetools plug-in, which saddles your HTML with URLs like this fella while Viteโs ropinโ in your assets:
<img src="/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520">
The vite-imagetools plug-inโs hopinโ that the output URL gets lassoed by Vite, then it can wrangle all URLs startinโ with /@imagetools. If you find yerself usinโ plug-ins hankerinโ for this behavior, ya best saddle up and correct those URLs yourself!
You can do this in your trusty vite.config.js file by ridinโ the transformOnServe pony:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { imagetools } from 'vite-imagetools';
export default defineConfig({
plugins: [
laravel({
// ...
transformOnServe: (code, devServerUrl) => code.replaceAll('/@imagetools', devServerUrl+'/@imagetools'),
}),
imagetools(),
],
});
Now, while Viteโs ropinโ in your assets, itโll lasso URLs pointinโ to the Vite dev server:
- <img src="/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520"><!-- [tl! remove] -->
+ <img src="http://[::1]:5173/@imagetools/f0b2f404b13f052c604e632f2fb60381bf61a520"><!-- [tl! add] -->
And there ya have it, partner! Keep those URLs in line and your Laravel-Vite integration will be as smooth as a freshly ironed duster! ๐ค ๐ธ๐