Skip to content

Commit a61f12b

Browse files
[11.x] Add CORS configuration to Vite plugin docs (#10131)
* Add CORS configuration to Vite plugin docs * wording --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d83edfc commit a61f12b

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

vite.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [Subresource Integrity (SRI)](#subresource-integrity-sri)
2929
- [Arbitrary Attributes](#arbitrary-attributes)
3030
- [Advanced Customization](#advanced-customization)
31+
- [Dev Server Cross-Origin Resource Sharing (CORS)](#cors)
3132
- [Correcting Dev Server URLs](#correcting-dev-server-urls)
3233

3334
<a name="introduction"></a>
@@ -928,6 +929,72 @@ export default defineConfig({
928929
});
929930
```
930931

932+
<a name="cors"></a>
933+
### Dev Server Cross-Origin Resource Sharing (CORS)
934+
935+
If you are experiencing Cross-Origin Resource Sharing (CORS) issues in the browser while fetching assets from the Vite dev server, you may need to grant your custom origin access to the dev server. Vite combined with the Laravel plugin allows the following origins without any additional configuration:
936+
937+
- `::1`
938+
- `127.0.0.1`
939+
- `localhost`
940+
- `*.test`
941+
- `*.localhost`
942+
- `APP_URL` in the project's `.env`
943+
944+
The easiest way to allow a custom origin for your project is to ensure that your application's `APP_URL` environment variable matches the origin you are visiting in your browser. For example, if you visiting `https://my-app.laravel`, you should update your `.env` to match:
945+
946+
```env
947+
APP_URL=https://my-app.laravel
948+
```
949+
950+
If you need more fine-grained control over the origins, such as supporting multiple origins, you should utilize [Vite's comprehensive and flexible built-in CORS server configuration](https://vite.dev/config/server-options.html#server-cors). For example, you may specify multiple origins in the `server.cors.origin` configuration option in the project's `vite.config.js` file:
951+
952+
```js
953+
import { defineConfig } from 'vite';
954+
import laravel from 'laravel-vite-plugin';
955+
956+
export default defineConfig({
957+
plugins: [
958+
laravel({
959+
input: 'resources/js/app.js',
960+
refresh: true,
961+
}),
962+
],
963+
server: { // [tl! add]
964+
cors: { // [tl! add]
965+
origin: [ // [tl! add]
966+
'https://backend.laravel', // [tl! add]
967+
'http://admin.laravel:8566', // [tl! add]
968+
], // [tl! add]
969+
}, // [tl! add]
970+
}, // [tl! add]
971+
});
972+
```
973+
974+
You may also include regex patterns, which can be helpful if you would like to allow all origins for a given top-level domain, such as `*.laravel`:
975+
976+
```js
977+
import { defineConfig } from 'vite';
978+
import laravel from 'laravel-vite-plugin';
979+
980+
export default defineConfig({
981+
plugins: [
982+
laravel({
983+
input: 'resources/js/app.js',
984+
refresh: true,
985+
}),
986+
],
987+
server: { // [tl! add]
988+
cors: { // [tl! add]
989+
origin: [ // [tl! add]
990+
// Supports: SCHEME://DOMAIN.laravel[:PORT] [tl! add]
991+
/^https?:\/\/.*\.laravel(:\d+)?$/, //[tl! add]
992+
], // [tl! add]
993+
}, // [tl! add]
994+
}, // [tl! add]
995+
});
996+
```
997+
931998
<a name="correcting-dev-server-urls"></a>
932999
### Correcting Dev Server URLs
9331000

0 commit comments

Comments
 (0)