├── src └── vite-plugins-cross-origin-isolation.mjs ├── package.json ├── LICENSE └── README.md /src/vite-plugins-cross-origin-isolation.mjs: -------------------------------------------------------------------------------- 1 | const crossOriginIsolation = () => ({ 2 | name: 'configure-server', 3 | 4 | configureServer(server) { 5 | server.middlewares.use((_req, res, next) => { 6 | res.setHeader("Cross-Origin-Opener-Policy", "same-origin"); 7 | res.setHeader("Cross-Origin-Embedder-Policy", "require-corp"); 8 | next(); 9 | }); 10 | } 11 | 12 | }); 13 | 14 | export default crossOriginIsolation -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-plugin-cross-origin-isolation", 3 | "version": "0.1.6", 4 | "type": "module", 5 | "description": "A vite plugin for enabling cross-origin-isolation for dev server.", 6 | "main": "src/vite-plugins-cross-origin-isolation.mjs", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/chaosprint/vite-plugin-cross-origin-isolation.git" 13 | }, 14 | "keywords": ["Vite", "SharedArrayBuffer", "CrossOriginIsolation", "COOP", "COEP", "CrossOriginIsolated"], 15 | "author": "Qichao Lan (chaosprint)", 16 | "license": "MIT" 17 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020-present Qichao Lan (chaopsrint) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vite-plugin-cross-origin-isolation 2 | 3 | A vite plugin for enabling cross-origin-isolation for dev server. 4 | 5 | ## Why 6 | 7 | `Cross origin isolation` is required for `SharedArrayBuffer`. 8 | 9 | Using this plugin, in local dev server, functions that rely on SAB can be enabled and tested. 10 | 11 | > Edit 2023: 12 | > 13 | > The motivation of this plugin was mainly for my project [Glicol: a collaborative music programming language](https://glicol.org) 14 | > 15 | > But now there are some "official" ways to do it, see [here](https://github.com/chaosprint/vite-plugin-cross-origin-isolation/issues/3). 16 | 17 | 18 | ## Usage 19 | 20 | First install it in your vite project: 21 | 22 | `npm i -D vite-plugin-cross-origin-isolation` 23 | 24 | Then, in `vite.config.js`: 25 | 26 | ``` 27 | import crossOriginIsolation from 'vite-plugin-cross-origin-isolation' 28 | 29 | export default { 30 | plugins: [ 31 | // other plugins... 32 | crossOriginIsolation() 33 | ] 34 | } 35 | ``` 36 | 37 | If this doesn't work, try this instead: 38 | ``` 39 | import { defineConfig } from "vite"; 40 | 41 | export default defineConfig({ 42 | plugins: [ 43 | { 44 | name: "configure-response-headers", 45 | configureServer: (server) => { 46 | server.middlewares.use((_req, res, next) => { 47 | res.setHeader("Cross-Origin-Embedder-Policy", "require-corp"); 48 | res.setHeader("Cross-Origin-Opener-Policy", "same-origin"); 49 | next(); 50 | }); 51 | }, 52 | }, 53 | ], 54 | }); 55 | ``` 56 | > Copy from: https://github.com/vitejs/vite/issues/3909#issuecomment-934044912 57 | 58 | ## License 59 | The MIT License (MIT) 60 | 61 | Copyright (c) 2021 - present Qichao Lan (chaosprint) 62 | --------------------------------------------------------------------------------