DEV Community

Cover image for Serving Single-Page Application in a single binary file with Go

Serving Single-Page Application in a single binary file with Go

Arya Prakasa on June 19, 2022

Although there are many alternatives to deploy a single-page application (SPA), you might find a situation where you need to deploy it in an isolat...
Collapse
 
besworks profile image
Besworks

I just started learning Go recently and found this post to contain a lot of useful information. I really feel like I could build a proper standalone web app after reading it. Thanks especially for the mentions of Echo and Fibre! Coming from Node as my primary backend it's nice to see such a familiar interface.

Collapse
 
aryaprakasa profile image
Arya Prakasa

Thanks, glad you find it useful!
In some circumstances, the standalone app is suitable for some cases, but not always.
That's the main point of frameworks, to make our lives easier.

Collapse
 
mrwormhole profile image
Talha Altınel

amazing post!

Collapse
 
abiiranathan profile image
Dr. Abiira Nathan

Nice article over here. Do you have an idea on to make go://embed include lazy loaded sveltekit endpoints that start with . e.g _id.indexeadfg.js?

If my SPA has hundreds of these, I cant include them one by one.

Collapse
 
aryaprakasa profile image
Arya Prakasa

Hi thanks,

You can do something like these:

//go:embed build/*
//go:embed build/*/*/*/*
//go:embed build/*/*/*/*/*
Enter fullscreen mode Exit fullscreen mode

Or explicitly embed specific file type:

//go:embed build/*.js
//go:embed build/*/*/*/*.js
//go:embed build/*/*/*/*/*.js
Enter fullscreen mode Exit fullscreen mode
Collapse
 
abiiranathan profile image
Dr. Abiira Nathan

That was actually helpful.

Collapse
 
aryaprakasa profile image
Arya Prakasa

Thanks, nice little snippets

Collapse
 
jeffryluqman profile image
Jeffry Luqman

i try to follow with echo but i got an error like this, how to solve this?

Image description

this is generated static file

Image description

Collapse
 
aryaprakasa profile image
Arya Prakasa

Have you try to prefix all: for the go embed directive? github.com/aprakasa/go-embed-spa/b...

Collapse
 
jeffryluqman profile image
Jeffry Luqman

yes, case closed , thx

it would be nice if the article was also revised

from

// Embed the build directory from the frontend.
//go:embed build/*
//go:embed build/_app/immutable/pages/*
//go:embed build/_app/immutable/assets/pages/*
Enter fullscreen mode Exit fullscreen mode

to

// Embed the build directory from the frontend.
//go:embed all:build
Enter fullscreen mode Exit fullscreen mode
Collapse
 
devappbox profile image
devappbox • Edited

Hello,

In your repo github.com/aprakasa/go-embed-spa,
I got error when adding file +page.server.js in routes/about folder.

+page.server.js
/** @type {import('./$types').PageServerLoad} */

export async function load() {
return {}
}

Here's the error

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

start-ce5b08e9.js:1 TypeError: Failed to fetch dynamically imported module: http://localhost:5050/about/__data.js?__invalid=ny&__id=1

Can you give me a workaround for this case ?

Thank You.

Collapse
 
lexiebkm profile image
Alexander B.K.

Which one do you recommend of Go framework : Fiber or Echo ? What about the others like Gin, Iris, etc ?

Collapse
 
a2yd5l profile image
Andy Donnell

Fantastic write-up! Thanks for sharing! :)