Add a random query param to force reload of script src on smart navigation

This commit is contained in:
Mikael Svenson 2019-03-18 08:56:39 +01:00
parent 71b03b4db2
commit 8c975dac6b
3 changed files with 8 additions and 19 deletions

View File

@ -39,12 +39,6 @@ The web part works by loading each script in a `<script src>` tag sequentially i
![site page header configurator web part](./assets/modern-script-editor-wp.gif)
If you for some reason need the script to force load on each new page, add the attribute `reload` to the script tag.
This will reload the script even on smart modern page loads.
```html
<script src="myscript.js" reload></script>
```
If all you want is to add markup on the page, you can do that as well. Adding the following html would show a headline and a list.
```html

View File

@ -3,7 +3,7 @@
"solution": {
"name": "Modern Script Editor web part by Puzzlepart",
"id": "1425175f-3ed8-44d2-8fc4-dd1497191294",
"version": "1.0.0.10",
"version": "1.0.0.11",
"includeClientSideAssets": true,
"skipFeatureDeployment": false
},

View File

@ -13,6 +13,7 @@ import { IScriptEditorWebPartProps } from './IScriptEditorWebPartProps';
import PropertyPaneLogo from './PropertyPaneLogo';
export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEditorWebPartProps> {
public save: (script: string) => void = (script: string) => {
this.properties.script = script;
this.render();
@ -149,14 +150,10 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
const urls = [];
const onLoads = [];
const forceReload = [];
for (let i = 0; scripts[i]; i++) {
const scriptTag = scripts[i];
if (scriptTag.src && scriptTag.src.length > 0) {
urls.push(scriptTag.src);
if (scriptTag.attributes["reload"]) {
forceReload.push(scriptTag.src);
}
}
if (scriptTag.onload && scriptTag.onload.length > 0) {
onLoads.push(scriptTag.onload);
@ -172,14 +169,12 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
for (let i = 0; i < urls.length; i++) {
try {
const scriptUrl = urls[i];
try {
if (forceReload.indexOf(scriptUrl) !== -1) {
let hackReload = (<any>SPComponentLoader)._instance;
hackReload._systemJsLoader.delete(urls[i]);
let scriptUrl = urls[i];
if (this.renderedOnce) {
let prefix = scriptUrl.indexOf('?') === -1 ? '?' : '&';
scriptUrl += prefix + 'cow=' + new Date().getTime();
}
} catch (silent) { }
await SPComponentLoader.loadScript(urls[i], { globalExportsName: "ScriptGlobal" });
await SPComponentLoader.loadScript(scriptUrl, { globalExportsName: "ScriptGlobal" });
} catch (error) {
if (console.error) {
console.error(error);