nue icon indicating copy to clipboard operation
nue copied to clipboard

external libraries in <head> in layout.html have errors (Pocketbase & AlpineJS)

Open bartonhammond opened this issue 11 months ago • 4 comments

Describe the Bug

I'm combining these two external library failures 'cause I suspect the solution will be similar...

I am trying to add both Pocketbase JS SDK https://pocketbase.io/ and AlpineJS https://alpinejs.dev/. I added them to the <head> in layout.html see https://github.com/bartonhammond/finance/blob/main/%40global/layout.html

The instructions are from https://www.npmjs.com/package/pocketbase as

Browser (manually via script tag)
<script src="/path/to/dist/pocketbase.umd.js"></script>
<script type="text/javascript">
    const pb = new PocketBase("https://example.com")
    ...
</script>

OR if you are using ES modules:

<script type="module">
    import PocketBase from '/path/to/dist/pocketbase.es.mjs' 
    const pb = new PocketBase("https://example.com")
    ...
</script>

Errors

*. Pocketbase When my component loads I see this in console (I tried both) Uncaught SyntaxError: Unexpected token 'export' (at pocketbase.umd.js:1096:1) or Uncaught SyntaxError: Unexpected token 'export' (at pocketbase.es.mjs:1:39456) *. Alpine The https://alpinejs.dev/start-here Alpine start provides this as proof it's loaded which I have in my component https://github.com/bartonhammond/finance/blob/main/components/listTransactions.dhtml#L13

 <h1 x-data="{ message: 'I ❤️ Alpine' }" x-text="message"></h1>

And this error

cdn.min.js:1 Alpine Expression Error: message is not defined
Expression: "message"

Environment

  • OS: Mac Sonoma
  • Nue 1.0.0-RC.2 • Bun 1.2.0

Minimal Reproduction

https://github.com/bartonhammond/finance

Logs & Additional Context

✓ Nue 1.0.0-RC.2 • Bun 1.2.0 
✓ Serving site from .dist/dev
✓ http://localhost:8080/ 
✓ /@global/colors.css 
✓ /@global/layout.css 
✓ /@global/typography.css 
✓ /style/todo.css 
✓ /@library/forms.css 
✓ index.html 

bartonhammond avatar Jan 24 '25 21:01 bartonhammond

I cannot see this repository: https://github.com/bartonhammond/finance/

The error seems to fire in pocketbase.umd.js — any thoughts/guesses how this relates to Nue?

Thanks

tipiirai avatar Jan 27 '25 07:01 tipiirai

Sorry, here: https://github.com/bartonhammond/finance-nue

I have no idea why this is related to Nue. I searched https://github.com/pocketbase/js-sdk/issues and didn't find anything related.

I use pocketbase here w/ an express project. This is exactly the same file in the nue project - I copied it here.

<!DOCTYPE html>
<html lang="en"  data-theme="dark" >
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}}</title>
    
    <script src="/js/pocketbase.umd.js"></script> <//////////////////////////////////////////////
    
    <script src="https://unpkg.com/[email protected]"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.lime.min.css"
      />
    <link rel="stylesheet"
	  href="css/style.css"/>
  </head>
  <script>
    const POCKETBASE_URL= "{{{POCKETBASE_URL}}}"
  </script>
  <body class="container" >
    {{> header  }}
    {{{body}}}
    <br>
    {{> footer }}
  </body>

</html>

bartonhammond avatar Jan 27 '25 12:01 bartonhammond

Sorry, that I didn't check this out until now, I'll probably take a closer look later today.

I think what happened here:

cdn.min.js:1 Alpine Expression Error: message is not defined
Expression: "message"

is, that nuekit replaced the contents of { message: 'I ❤️ Alpine' }, because it's between the {} delimiters, and message then is undefined when being parsed by alpine.

This is just a suspicion though.

nobkd avatar Mar 18 '25 00:03 nobkd

You can probably use the node package, and let it be bundled. Create e.g. @global/mypocktebase.js, import node module import PocketBase from 'pocketbase'; const pb = new PocketBase('http://localhost:8080'); console.log(pb) add bundle: ['mypocketbase.js'] to site.yaml, delete .dist, run dev server.


To have objects in attributes, you probably need to do something like this:

<div @name="component">
  <h1 x-data="{ JSON.stringify(myobject) }" x-text="message"></h1>
  
  <script>
  myobject = { message: 'I ❤️ Alpine' }
  </script>
</div>

nobkd avatar Mar 18 '25 17:03 nobkd