Skip to content

Commit 1ee274e

Browse files
authored
docs: add createLink hook desc and faq (#3785)
1 parent 1e24dc3 commit 1ee274e

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

apps/manifest-demo/webpack-host/runtimePlugin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ export default function (): FederationRuntimePlugin {
1212

1313
return args;
1414
},
15+
createLink({ url }) {
16+
const link = document.createElement('link');
17+
link.setAttribute('href', url);
18+
link.setAttribute('rel', 'preload');
19+
link.setAttribute('as', 'script');
20+
link.setAttribute('crossorigin', 'anonymous');
21+
return link;
22+
},
1523
};
1624
}

apps/website-new/docs/en/guide/troubleshooting/other.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,33 @@ When `exposes` is set in the project, it will be regarded as a producer. To ensu
9090
* [Modern.js]: Set [devServer.headers](https://modernjs.dev/configure/app/tools/dev-server.html#headers) value to the specified domain whitelist instead of `*`
9191

9292
* [Rsbuild]: Set [server.cors.origin](https://rsbuild.dev/config/server/cors#origin) value to the specified domain whitelist instead of `*`
93+
94+
## A preload for 'http://resource-url' is found, but is not used because the request credentials mode does not match. Consider taking a look at crossorigin attribute.
95+
96+
### Reason
97+
98+
When the producer URL is a manifest, loading this producer module will automatically preload the corresponding resources. If the above warning occurs, it is because the default preload does not configure credentials, while the actual `load remote script` carries the corresponding credentials, causing the preload to fail.
99+
100+
### Solution
101+
102+
Add a runtime plugin via [runtimePlugins](../../configure/runtimeplugins) and configure the `crossorigin` attribute in the [createLink](../../plugin/dev/index#createlink) hook. Its value needs to be consistent with the actual `load script`.
103+
104+
For example, to modify the crossorigin attribute of the preloaded link to `anonymous`:
105+
106+
```ts title="runtimePlugin.ts
107+
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
108+
109+
export default function MFLinkPlugin(): FederationRuntimePlugin {
110+
return {
111+
name: 'link-plugin',
112+
createLink({ url }) {
113+
const link = document.createElement('link');
114+
link.setAttribute('href', url);
115+
link.setAttribute('rel', 'preload');
116+
link.setAttribute('as', 'script');
117+
link.setAttribute('crossorigin', 'anonymous');
118+
return link
119+
}
120+
};
121+
}
122+
```

apps/website-new/docs/en/plugin/dev/index.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,42 @@ const changeScriptAttributePlugin: () => FederationRuntimePlugin = function () {
534534
};
535535
```
536536

537+
538+
### createLink
539+
540+
`SyncHook`
541+
542+
- **Type**
543+
544+
```typescript
545+
function createLink(args: CreateLinkOptions): HTMLLinkElement | void;
546+
547+
type CreateScriptOptions = {
548+
url: string;
549+
attrs?: Record<string, any>;
550+
};
551+
```
552+
553+
- Example
554+
555+
```typescript
556+
import { init } from '@module-federation/enhanced/runtime';
557+
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
558+
559+
const changeLinkAttributePlugin: () => FederationRuntimePlugin = function () {
560+
return {
561+
name: 'change-link-attribute',
562+
createLink({ url }) {
563+
link.setAttribute('href', url);
564+
link.setAttribute('rel', 'preload');
565+
link.setAttribute('as', 'script');
566+
link.setAttribute('crossorigin', 'anonymous');
567+
return link;
568+
},
569+
};
570+
};
571+
```
572+
537573
### fetch
538574
The `fetch` function allows customizing the request that fetches the manifest JSON. A successful `Response` must yield a valid JSON.
539575

apps/website-new/docs/zh/guide/troubleshooting/other.mdx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,33 @@ Uncaught TypeError: Cannot read properties on null (reading `useState`)
6464
* [Modern.js]: 设置 [devServer.headers](https://modernjs.dev/configure/app/tools/dev-server.html#headers) 值为指定的域名白名单而非 `*`
6565

6666
* [Rsbuild]: 设置 [server.cors.origin](https://rsbuild.dev/config/server/cors#origin) 值为指定的域名白名单而非 `*`
67+
68+
## A preload for 'http://resource-url' is found, but is not used because the request credentials mode does not match. Consider taking a look at crossorigin attribute.
69+
70+
### 原因
71+
72+
当生产者 url 为 manifest 时,加载此生产者模块会自动预加载相应的资源。如果出现上述警告,是因为默认预加载不会配置 credentials ,而实际的 load remote script 携带了对应的 credentials ,导致预加载失败。
73+
74+
### 解决方案
75+
76+
通过 [runtimePlugins](../../configure/runtimeplugins) 添加运行时插件,在 [createLink](../../plugin/dev/index#createlink) hook 中配置 `crossorigin` 属性,其值需要和实际 load script 保持一致。
77+
78+
例如需要修改预加载 link 的 crossorigin 属性为 `anonymous`
79+
80+
```ts title="runtimePlugin.ts
81+
import { FederationRuntimePlugin } from '@module-federation/runtime/types';
82+
83+
export default function MFLinkPlugin(): FederationRuntimePlugin {
84+
return {
85+
name: 'link-plugin',
86+
createLink({ url }) {
87+
const link = document.createElement('link');
88+
link.setAttribute('href', url);
89+
link.setAttribute('rel', 'preload');
90+
link.setAttribute('as', 'script');
91+
link.setAttribute('crossorigin', 'anonymous');
92+
return link
93+
}
94+
};
95+
}
96+
```

apps/website-new/docs/zh/plugin/dev/index.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,73 @@ const changeScriptAttributePlugin: () => FederationRuntimePlugin = function () {
532532
};
533533
```
534534

535+
536+
### createLink
537+
538+
`SyncHook`
539+
540+
- 类型
541+
542+
```typescript
543+
function createLink(args: CreateLinkOptions): HTMLLinkElement | void;
544+
545+
type CreateScriptOptions = {
546+
url: string;
547+
attrs?: Record<string, any>;
548+
};
549+
```
550+
551+
- 示例
552+
553+
```typescript
554+
import { init } from '@module-federation/enhanced/runtime';
555+
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
556+
557+
const changeLinkAttributePlugin: () => FederationRuntimePlugin = function () {
558+
return {
559+
name: 'change-link-attribute',
560+
createLink({ url }) {
561+
link.setAttribute('href', url);
562+
link.setAttribute('rel', 'preload');
563+
link.setAttribute('as', 'script');
564+
link.setAttribute('crossorigin', 'anonymous');
565+
return link;
566+
},
567+
};
568+
};
569+
```
570+
571+
### fetch
572+
573+
`fetch` 函数用于自定义获取 Manifest 的请求。
574+
575+
`AsyncHook`
576+
577+
- **类型**
578+
579+
```typescript
580+
function fetch(manifestUrl: string, requestInit: RequestInit): Promise<Response> | void | false;
581+
```
582+
583+
- 示例:获取 manifest 时增加 credentials:
584+
585+
```typescript
586+
// fetch-manifest-with-credentials-plugin.ts
587+
import type { FederationRuntimePlugin } from '@module-federation/enhanced/runtime';
588+
589+
export default function (): FederationRuntimePlugin {
590+
return {
591+
name: 'fetch-manifest-with-credentials-plugin',
592+
fetch(manifestUrl, requestInit) {
593+
return fetch(manifestUrl, {
594+
...requestInit,
595+
credentials: 'include'
596+
});
597+
},
598+
}
599+
};
600+
```
601+
535602
### loadEntry
536603
可以完全自定义remote, 可以扩展新的remote类型。
537604
下面两个简单的例子分别实现了加载json数据和模块代理

0 commit comments

Comments
 (0)