From fa7b11198eb0da1d6d617d24da3a0f340c2e5e7c Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Mon, 7 Oct 2024 16:38:10 -0400 Subject: [PATCH] feat: implement CDN support for assets --- pkg/nebula/assets/css/styles.css | 4 - pkg/nebula/global/state/worker.templ | 99 ++++------- pkg/nebula/global/state/worker_templ.go | 212 ++++++++++++++++-------- 3 files changed, 171 insertions(+), 144 deletions(-) diff --git a/pkg/nebula/assets/css/styles.css b/pkg/nebula/assets/css/styles.css index d196e89da..eeb0289af 100644 --- a/pkg/nebula/assets/css/styles.css +++ b/pkg/nebula/assets/css/styles.css @@ -1797,10 +1797,6 @@ select{ background-color: rgb(24 24 27 / var(--tw-bg-opacity)); } -.bg-opacity-50{ - --tw-bg-opacity: 0.5; -} - .bg-opacity-90{ --tw-bg-opacity: 0.9; } diff --git a/pkg/nebula/global/state/worker.templ b/pkg/nebula/global/state/worker.templ index 10e555e26..9d4c3b963 100644 --- a/pkg/nebula/global/state/worker.templ +++ b/pkg/nebula/global/state/worker.templ @@ -4,9 +4,22 @@ const ( CDNPath = "https://cdn.sonr.id" ) -var serviceWorkerHandle = templ.NewOnceHandle() +var ( + serviceWorkerHandle = templ.NewOnceHandle() + stylesHandle = templ.NewOnceHandle() + alpineHandle = templ.NewOnceHandle() + htmxHandle = templ.NewOnceHandle() +) -script RegisterServiceWorker() { +func cdnPath(path string, local bool) string { + if local { + return path + } + return CDNPath + path +} + +templ RegisterServiceWorker() { + } -templ ImportScripts() { - - - +templ Styles(local bool) { + @stylesHandle.Once() { + + } } -templ indexFile(cfg string) { - - - - - - - - @importScripts() - - Sonr DWN - - - -
-
Loading...
-
- @ServiceWorkerHandle.Once() { - - @initializeServiceWorker(cfg) - } - - +templ Alpine(local bool) { + @alpineHandle.Once() { + + } } -script initializeServiceWorker(config string) { - const serviceWorker = new ServiceWorker(JSON.parse(config)); - - async function demo() { - try { - // Insert a new account - const accountId = await serviceWorker.insertAccount({ - name: 'John Doe', - address: '0x1234567890123456789012345678901234567890', - balance: 100, - }); - - console.log('Account created:', accountId); - - // Get the account - const account = await serviceWorker.getAccount(accountId); - - console.log('Account:', account); - - } - catch (error) { - console.error('Error:', error); - } - } +templ Htmx(local bool) { + @htmxHandle.Once() { + + } } + diff --git a/pkg/nebula/global/state/worker_templ.go b/pkg/nebula/global/state/worker_templ.go index ef99e3024..54287461b 100644 --- a/pkg/nebula/global/state/worker_templ.go +++ b/pkg/nebula/global/state/worker_templ.go @@ -12,30 +12,21 @@ const ( CDNPath = "https://cdn.sonr.id" ) -var serviceWorkerHandle = templ.NewOnceHandle() +var ( + serviceWorkerHandle = templ.NewOnceHandle() + stylesHandle = templ.NewOnceHandle() + alpineHandle = templ.NewOnceHandle() + htmxHandle = templ.NewOnceHandle() +) -func RegisterServiceWorker() templ.ComponentScript { - return templ.ComponentScript{ - Name: `__templ_RegisterServiceWorker_a2fb`, - Function: `function __templ_RegisterServiceWorker_a2fb(){if ("serviceWorker" in navigator) { - window.addEventListener("load", function() { - navigator.serviceWorker - .register("/sw.js") - .then(function (registration) { - console.log("Service Worker registered with scope:", registration.scope); - }) - .catch(function (error) { - console.log("Service Worker registration failed:", error); - }); - }); - } -}`, - Call: templ.SafeScript(`__templ_RegisterServiceWorker_a2fb`), - CallInline: templ.SafeScriptInline(`__templ_RegisterServiceWorker_a2fb`), +func cdnPath(path string, local bool) string { + if local { + return path } + return CDNPath + path } -func ImportScripts() templ.Component { +func RegisterServiceWorker() templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -56,7 +47,7 @@ func ImportScripts() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -64,7 +55,7 @@ func ImportScripts() templ.Component { }) } -func indexFile(cfg string) templ.Component { +func Styles(local bool) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { @@ -85,18 +76,6 @@ func indexFile(cfg string) templ.Component { templ_7745c5c3_Var2 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = importScripts().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Sonr DWN
Loading...
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } templ_7745c5c3_Var3 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) @@ -109,21 +88,26 @@ func indexFile(cfg string) templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" ") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = ServiceWorkerHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var3), templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + templ_7745c5c3_Err = stylesHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var3), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -131,36 +115,124 @@ func indexFile(cfg string) templ.Component { }) } -func initializeServiceWorker(config string) templ.ComponentScript { - return templ.ComponentScript{ - Name: `__templ_initializeServiceWorker_8c3c`, - Function: `function __templ_initializeServiceWorker_8c3c(config){const serviceWorker = new ServiceWorker(JSON.parse(config)); +func Alpine(local bool) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = alpineHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} - async function demo() { - try { - // Insert a new account - const accountId = await serviceWorker.insertAccount({ - name: 'John Doe', - address: '0x1234567890123456789012345678901234567890', - balance: 100, - }); - - console.log('Account created:', accountId); - - // Get the account - const account = await serviceWorker.getAccount(accountId); - - console.log('Account:', account); - - } - catch (error) { - console.error('Error:', error); - } - } -}`, - Call: templ.SafeScript(`__templ_initializeServiceWorker_8c3c`, config), - CallInline: templ.SafeScriptInline(`__templ_initializeServiceWorker_8c3c`, config), - } +func Htmx(local bool) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var8 := templ.GetChildren(ctx) + if templ_7745c5c3_Var8 == nil { + templ_7745c5c3_Var8 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var9 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = htmxHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var9), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) } var _ = templruntime.GeneratedTemplate