Skip to content

Commit

Permalink
Different attempt to fix #285 (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
clayallsopp authored Sep 5, 2024
1 parent 2017a6a commit a2d78c1
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ clean:

.PHONY: build
build: clean
yarn patch-package
@$(ROLLUP) -c


Expand Down Expand Up @@ -53,6 +54,7 @@ lint-fix:
.PHONY: setup
setup:
yarn
yarn patch-package


# .PHONY: test
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
"husky": "^4.2.3",
"jest": "^27.5.1",
"mocha": "2.3.x",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"prettier": "^1.19.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
21 changes: 21 additions & 0 deletions patches/react-script-hook+1.6.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/node_modules/react-script-hook/lib/use-script.js b/node_modules/react-script-hook/lib/use-script.js
index b7ba6fc..e66de4b 100644
--- a/node_modules/react-script-hook/lib/use-script.js
+++ b/node_modules/react-script-hook/lib/use-script.js
@@ -93,6 +93,16 @@ function useScript(_a) {
return function () {
scriptEl.removeEventListener('load', handleLoad);
scriptEl.removeEventListener('error', handleError);
+
+ // if we unmount, and we are still loading the script, then
+ // remove from the DOM & cache so we have a clean slate next time.
+ // this is similar to the `removeOnUnmount` behavior of the TS useScript hook
+ // https://github.com/juliencrn/usehooks-ts/blob/20667273744a22dd2cd2c48c38cd3c10f254ae47/packages/usehooks-ts/src/useScript/useScript.ts#L134
+ // but only applied when loading
+ if (status && status.loading) {
+ scriptEl.remove();
+ delete exports.scripts[src];
+ }
};
// we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
}, [src]);
Loading

0 comments on commit a2d78c1

Please sign in to comment.