Skip to content

Commit 09a81db

Browse files
authored
docs(browser): add docs for response header settings and known issues (#11523)
* zh * en * update * Add examples
1 parent 21cd3aa commit 09a81db

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

website/docs/en/api/javascript-api/browser.mdx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ rspack({}, (err, stats) => {
2929
});
3030
```
3131

32+
## Response header settings
33+
34+
Note that `@rspack/browser` internally uses [SharedArrayBuffer](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) to implement shared memory across multiple threads. Therefore, you need to set response headers for both your development server and production deployment environment.
35+
36+
If you are using Rspack as your project's bundler, you can set it through [devServer.headers](/config/dev-server#devserverheaders):
37+
38+
```js title="rspack.config.mjs"
39+
export default {
40+
devServer: {
41+
headers: {
42+
'Cross-Origin-Opener-Policy': 'same-origin',
43+
'Cross-Origin-Embedder-Policy': 'require-corp',
44+
},
45+
},
46+
};
47+
```
48+
49+
If you are using Rsbuild as your project's bundler, you can set it through [server.headers](https://rsbuild.rs/config/server/headers#serverheaders):
50+
51+
```ts title="rsbuild.config.ts"
52+
export default {
53+
server: {
54+
headers: {
55+
'Cross-Origin-Opener-Policy': 'same-origin',
56+
'Cross-Origin-Embedder-Policy': 'require-corp',
57+
},
58+
},
59+
};
60+
```
61+
62+
For production environments, please refer to the documentation of your project's deployment platform.
63+
3264
## In-Memory File system
3365

3466
Since browsers cannot directly access the local file system, `@rspack/browser` provides an in-memory file system object `builtinMemFs` based on [memfs](https://www.npmjs.com/package/memfs) for reading and writing files in the browser environment. All file system reads and writes in both the Node.js and Rust sides are redirected to this in-memory file system, including reading project configuration, source code, `node_modules` dependencies, and writing output files.
@@ -158,3 +190,26 @@ If your project does not require dynamic loading and execution of JavaScript cod
158190

159191
If your project does not distribute untrusted code or distributing such code does not cause security issues, you can directly use `BrowserRequirePlugin.unsafeExecute`. For example, the [Rspack Playground](https://playground.rspack.rs/) does not involve user privacy or account security.
160192
:::
193+
194+
## Known issues
195+
196+
- [napi-rs#2867](https://github.com/napi-rs/napi-rs/issues/2867): Currently, in projects using `@rspack/browser`, you need to inject `process.env.NODE_DEBUG_NATIVE`.
197+
If you are using Rspack to bundle your project, you can set it with [DefinePlugin](/plugins/webpack/define-plugin):
198+
199+
```js title="rspack.config.mjs"
200+
new rspack.DefinePlugin({
201+
'process.env.NODE_DEBUG_NATIVE': JSON.stringify(false),
202+
});
203+
```
204+
205+
if you are using Rsbuild to bundle your project, you can set it with [source.define](https://rsbuild.rs/config/source/define#sourcedefine):
206+
207+
```ts title="rsbuild.config.ts"
208+
export default {
209+
source: {
210+
define: {
211+
'process.env.NODE_DEBUG_NATIVE': JSON.stringify(false),
212+
},
213+
},
214+
};
215+
```

website/docs/zh/api/javascript-api/browser.mdx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ rspack({}, (err, stats) => {
2929
});
3030
```
3131

32+
## 响应头设置
33+
34+
`@rspack/browser` 内部使用了 [SharedArrayBuffer](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) 来实现多线程的共享内存,因此你需要为开发服务器或线上部署环境设置响应头。
35+
36+
如果你正在使用 Rspack 作为项目的打包器,可以通过 [devServer.headers](/config/dev-server#devserverheaders) 设置:
37+
38+
```js title="rspack.config.mjs"
39+
export default {
40+
//...
41+
devServer: {
42+
headers: {
43+
'Cross-Origin-Opener-Policy': 'same-origin',
44+
'Cross-Origin-Embedder-Policy': 'require-corp',
45+
},
46+
},
47+
};
48+
```
49+
50+
如果你正在使用 Rsbuild 作为项目的打包器,可以通过 [server.headers](https://rsbuild.rs/config/server/headers#serverheaders) 设置:
51+
52+
```ts title="rsbuild.config.ts"
53+
export default {
54+
server: {
55+
headers: {
56+
'Cross-Origin-Opener-Policy': 'same-origin',
57+
'Cross-Origin-Embedder-Policy': 'require-corp',
58+
},
59+
},
60+
};
61+
```
62+
63+
对于线上环境,请参考你的项目部署平台的相关文档。
64+
3265
## 内存文件系统
3366

3467
由于浏览器无法直接访问本地文件系统,`@rspack/browser` 提供了基于 [memfs](https://www.npmjs.com/package/memfs) 的内存文件系统对象 `builtinMemFs`,用于在浏览器环境下读写文件。Node.js 和 Rust 层对文件系统的读写均会重定向到该内存文件系统,包括项目配置、源代码、`node_modules` 依赖及产物。
@@ -158,3 +191,26 @@ interface BrowserRequirePluginOptions {
158191

159192
若项目不会分发不可信代码,或即使分发也不会造成安全问题,可直接使用 `BrowserRequirePlugin.unsafeExecute`。例如 [Rspack Playground](https://playground.rspack.rs/) 就不涉及用户隐私或账户安全。
160193
:::
194+
195+
## 已知问题
196+
197+
- [napi-rs#2867](https://github.com/napi-rs/napi-rs/issues/2867): 目前你需要在使用了 `@rspack/browser` 的项目中注入 `process.env.NODE_DEBUG_NATIVE`
198+
如果你正在使用 Rspack 打包你的项目,那么可以使用 [DefinePlugin](/plugins/webpack/define-plugin)
199+
200+
```js title="rspack.config.mjs"
201+
new rspack.DefinePlugin({
202+
'process.env.NODE_DEBUG_NATIVE': JSON.stringify(false),
203+
});
204+
```
205+
206+
如果你正在使用 Rsbuild 打包你的项目,那么可以使用 [source.define](https://rsbuild.rs/config/source/define#sourcedefine)
207+
208+
```ts title="rsbuild.config.ts"
209+
export default {
210+
source: {
211+
define: {
212+
'process.env.NODE_DEBUG_NATIVE': JSON.stringify(false),
213+
},
214+
},
215+
};
216+
```

0 commit comments

Comments
 (0)