changes to support page level scripts, ability to detect prerendering
This commit is contained in:
@ -15,8 +15,8 @@
|
||||
{
|
||||
<link id="app-manifest" rel="manifest" />
|
||||
}
|
||||
@Html.Raw(Model.HeadResources)
|
||||
<component type="typeof(Oqtane.Head)" render-mode="@((RenderMode)Enum.Parse(typeof(RenderMode), Model.RenderMode, true))" />
|
||||
@Html.Raw(Model.HeadResources)
|
||||
</head>
|
||||
<body>
|
||||
@if (string.IsNullOrEmpty(Model.Message))
|
||||
|
@ -19,7 +19,6 @@ using Microsoft.Extensions.Primitives;
|
||||
using Oqtane.Enums;
|
||||
using Oqtane.Security;
|
||||
using Oqtane.Extensions;
|
||||
using Oqtane.Themes;
|
||||
|
||||
namespace Oqtane.Pages
|
||||
{
|
||||
@ -130,10 +129,8 @@ namespace Oqtane.Pages
|
||||
{
|
||||
PWAScript = CreatePWAScript(alias, site, route);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(site.HeadContent))
|
||||
{
|
||||
ProcessHeadContent(site.HeadContent, "site");
|
||||
}
|
||||
// site level scripts
|
||||
HeadResources += ParseScripts(site.HeadContent);
|
||||
|
||||
// get jwt token for downstream APIs
|
||||
if (User.Identity.IsAuthenticated)
|
||||
@ -174,8 +171,6 @@ namespace Oqtane.Pages
|
||||
}
|
||||
}
|
||||
|
||||
ProcessHeadContent(page.HeadContent, $"page{page.PageId}");
|
||||
|
||||
// include global resources
|
||||
var assemblies = AppDomain.CurrentDomain.GetOqtaneAssemblies();
|
||||
foreach (Assembly assembly in assemblies)
|
||||
@ -427,27 +422,20 @@ namespace Oqtane.Pages
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessHeadContent(string headcontent, string id)
|
||||
private string ParseScripts(string headcontent)
|
||||
{
|
||||
// add scripts to page
|
||||
// iterate scripts
|
||||
var scripts = "";
|
||||
if (!string.IsNullOrEmpty(headcontent))
|
||||
{
|
||||
var count = 0;
|
||||
var index = headcontent.IndexOf("<script");
|
||||
while (index >= 0)
|
||||
{
|
||||
var script = headcontent.Substring(index, headcontent.IndexOf("</script>", index) + 9 - index);
|
||||
if (!script.Contains("src=") && !script.Contains("id="))
|
||||
{
|
||||
count += 1;
|
||||
id += $"-script{count}";
|
||||
script = script.Replace("<script", $"<script id=\"{id}\"");
|
||||
index += id.Length;
|
||||
}
|
||||
HeadResources += script + Environment.NewLine;
|
||||
scripts += headcontent.Substring(index, headcontent.IndexOf("</script>", index) + 9 - index);
|
||||
index = headcontent.IndexOf("<script", index + 1);
|
||||
}
|
||||
}
|
||||
return scripts;
|
||||
}
|
||||
|
||||
private void ProcessResource(Resource resource, int count, Alias alias)
|
||||
|
@ -116,6 +116,10 @@ Oqtane.Interop = {
|
||||
else {
|
||||
script = document.getElementById(id);
|
||||
}
|
||||
if (script !== null) {
|
||||
script.remove();
|
||||
script = null;
|
||||
}
|
||||
if (script === null) {
|
||||
script = document.createElement("script");
|
||||
if (id !== "") {
|
||||
@ -139,50 +143,22 @@ Oqtane.Interop = {
|
||||
script.async = false;
|
||||
this.addScript(script, location)
|
||||
.then(() => {
|
||||
console.log(src + ' loaded');
|
||||
if (src !== "") {
|
||||
console.log(src + ' loaded');
|
||||
}
|
||||
else {
|
||||
console.log(id + ' loaded');
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.error(src + ' failed');
|
||||
if (src !== "") {
|
||||
console.error(src + ' failed');
|
||||
}
|
||||
else {
|
||||
console.error(id + ' failed');
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (script.id !== id) {
|
||||
script.setAttribute('id', id);
|
||||
}
|
||||
if (type !== "") {
|
||||
if (script.type !== type) {
|
||||
script.setAttribute('type', type);
|
||||
}
|
||||
} else {
|
||||
script.removeAttribute('type');
|
||||
}
|
||||
if (src !== "") {
|
||||
if (script.src !== this.getAbsoluteUrl(src)) {
|
||||
script.removeAttribute('integrity');
|
||||
script.removeAttribute('crossorigin');
|
||||
script.src = src;
|
||||
}
|
||||
if (integrity !== "") {
|
||||
if (script.integrity !== integrity) {
|
||||
script.setAttribute('integrity', integrity);
|
||||
}
|
||||
} else {
|
||||
script.removeAttribute('integrity');
|
||||
}
|
||||
if (crossorigin !== "") {
|
||||
if (script.crossOrigin !== crossorigin) {
|
||||
script.setAttribute('crossorigin', crossorigin);
|
||||
}
|
||||
} else {
|
||||
script.removeAttribute('crossorigin');
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (script.innerHTML !== content) {
|
||||
script.innerHTML = content;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
addScript: function (script, location) {
|
||||
if (location === 'head') {
|
||||
@ -234,6 +210,10 @@ Oqtane.Interop = {
|
||||
if (path === scripts[s].href && scripts[s].es6module === true) {
|
||||
element.type = "module";
|
||||
}
|
||||
if (path === scripts[s].href && scripts[s].location === 'body') {
|
||||
document.body.appendChild(element);
|
||||
return false; // return false to bypass default DOM insertion mechanism
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user