Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useExternal): allow resources to remain after they have lost the… #2165

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/hooks/src/useExternal/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ describe('useExternal', () => {
unmount();
expect(document.querySelector('script')).toBeNull();
});
it('should not remove when keepWhenUnused is true', () => {
// https://github.com/alibaba/hooks/discussions/2163
const { result, unmount } = setup('b.js', {
keepWhenUnused: true,
});
const script = document.querySelector('script') as HTMLScriptElement;
act(() => {
fireEvent.load(script);
});
unmount();
expect(result.current).toBe('ready');
});

it('css preload should work in IE Edge', () => {
Object.defineProperty(HTMLLinkElement.prototype, 'hideFocus', {
Expand Down
11 changes: 6 additions & 5 deletions packages/hooks/src/useExternal/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const status = useExternal(path: string, options?: Options);

### Options

| Params | Description | Type | Default |
| ------ | -------------------------------------------------------------------------------------------------------------------- | ------------------- | ------- |
| type | The type of external resources which need to load, support `js`/`css`, if no type, it will deduced according to path | `string` | - |
| js | Attributes supported by `script` | `HTMLScriptElement` | - |
| css | Attributes supported by `link` | `HTMLStyleElement` | - |
| Params | Description | Type | Default |
| -------------- | -------------------------------------------------------------------------------------------------------------------- | ------------------- | ------- |
| type | The type of external resources which need to load, support `js`/`css`, if no type, it will deduced according to path | `string` | - |
| js | Attributes supported by `script` | `HTMLScriptElement` | - |
| css | Attributes supported by `link` | `HTMLStyleElement` | - |
| keepWhenUnused | Allow resources to remain after they have lost their references | `boolean` | `false` |
5 changes: 4 additions & 1 deletion packages/hooks/src/useExternal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { useEffect, useRef, useState } from 'react';
type JsOptions = {
type: 'js';
js?: Partial<HTMLScriptElement>;
keepWhenUnused?: boolean;
};

type CssOptions = {
type: 'css';
css?: Partial<HTMLStyleElement>;
keepWhenUnused?: boolean;
};

type DefaultOptions = {
type?: never;
js?: Partial<HTMLScriptElement>;
css?: Partial<HTMLStyleElement>;
keepWhenUnused?: boolean;
};

export type Options = JsOptions | CssOptions | DefaultOptions;
Expand Down Expand Up @@ -138,7 +141,7 @@ const useExternal = (path?: string, options?: Options) => {

EXTERNAL_USED_COUNT[path] -= 1;

if (EXTERNAL_USED_COUNT[path] === 0) {
if (EXTERNAL_USED_COUNT[path] === 0 && !options?.keepWhenUnused) {
ref.current?.remove();
}

Expand Down
11 changes: 6 additions & 5 deletions packages/hooks/src/useExternal/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const status = useExternal(path: string, options?: Options);

### Options

| 参数 | 说明 | 类型 | 默认值 |
| ---- | ----------------------------------------------------------------- | ------------------- | ------ |
| type | 需引入外部资源的类型,支持 `js`/`css`,如果不传,则根据 path 推导 | `string` | - |
| js | `script` 标签支持的属性 | `HTMLScriptElement` | - |
| css | `link` 标签支持的属性 | `HTMLStyleElement` | - |
| 参数 | 说明 | 类型 | 默认值 |
| -------------- | ----------------------------------------------------------------- | ------------------- | ------- |
| type | 需引入外部资源的类型,支持 `js`/`css`,如果不传,则根据 path 推导 | `string` | - |
| js | `script` 标签支持的属性 | `HTMLScriptElement` | - |
| css | `link` 标签支持的属性 | `HTMLStyleElement` | - |
| keepWhenUnused | 在不持有资源的引用后,仍然保留资源 | `boolean` | `false` |