plentico / plenti

Static Site Generator with Go backend and Svelte frontend
https://plenti.co
Apache License 2.0
1k stars 49 forks source link

404 error when trying to use Lifecycle functions #38

Closed jimafisk closed 3 years ago

jimafisk commented 4 years ago

It's inevitable that someone will run into this, so figured I'd document it here. If you try to import something from the base svelte package, like the documentation tells you to do for a lifecycle function like onMount:

<script>
  import { onMount } from 'svelte';
</script>

You will get a 404 error. This is due to Gopack not being smart enough to read this reference. You can get around this by simply referencing the exact package, which is "internal" in this case:

<script>
  import { onMount } from 'svelte/internal';
</script>
Here are the errors you will see in the browser
Error in Firefox: ``` Loading module from “http://localhost:3000/spa/web_modules/svelte/internal/” was blocked because of a disallowed MIME type (“text/html”). ``` Error in Chrome: ``` Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. ```
jimafisk commented 3 years ago

The problem here is we're trying to walk nested directories to find the file. The "svelte" base folder does have an index.js file that exports everything from the internal/ folder, but we start walking the nested directories before we get to that file, and find the index.js files within the nested folder first.

We do need to still check the subdirectories for libs that use a dist/ folder, like navaid, but we only want to do that after check the current directory for a file with .js extension.

We can check through current dir with ioutil.ReadDir(): https://stackoverflow.com/questions/14668850/list-directory-in-go

jimafisk commented 3 years ago

Fixed in v0.3.15