Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No wasm mode #810

Merged
merged 7 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/app/gen/app.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/*------------------------------------------------------------------------------
Loader
------------------------------------------------------------------------------*/
.goapp-app-info {
.app-wasm-loader-hidden {
display: none;
}

.app-wasm-loader {
position: fixed;
top: 0;
left: 0;
Expand Down
18 changes: 14 additions & 4 deletions pkg/app/gen/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,15 @@ function goappKeepBodyClean() {
// Web Assembly
// -----------------------------------------------------------------------------
async function goappInitWebAssembly() {
const loader = document.getElementById("app-wasm-loader");

if (!goappCanLoadWebAssembly()) {
document.getElementById("app-wasm-loader").style.display = "none";
loader.remove();
return;
}

loader.className = "app-wasm-loader";

let instantiateStreaming = WebAssembly.instantiateStreaming;
if (!instantiateStreaming) {
instantiateStreaming = async (resp, importObject) => {
Expand All @@ -219,6 +223,7 @@ async function goappInitWebAssembly() {
);

go.run(wasm.instance);
loader.remove();
} catch (err) {
loaderIcon.className = "goapp-logo";
loaderLabel.innerText = err;
Expand All @@ -227,9 +232,14 @@ async function goappInitWebAssembly() {
}

function goappCanLoadWebAssembly() {
return !/bot|googlebot|crawler|spider|robot|crawling/i.test(
navigator.userAgent
);
if (
/bot|googlebot|crawler|spider|robot|crawling/i.test(navigator.userAgent)
) {
return false;
}

const urlParams = new URLSearchParams(window.location.search);
return urlParams.get("wasm") !== "false";
}

async function fetchWithProgress(url, progess) {
Expand Down
15 changes: 7 additions & 8 deletions pkg/app/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io"
"reflect"
"strconv"

"github.com/maxence-charriere/go-app/v9/pkg/errors"
)
Expand Down Expand Up @@ -309,14 +310,13 @@ func (e *htmlElement) html(w io.Writer) {
io.WriteString(w, k)

if v != "" {
io.WriteString(w, `="`)
io.WriteString(w, resolveAttributeURLValue(k, v, func(s string) string {
io.WriteString(w, `=`)
io.WriteString(w, strconv.Quote(resolveAttributeURLValue(k, v, func(s string) string {
if e.dispatcher != nil {
return e.dispatcher.resolveStaticResource(v)
}
return v
}))
io.WriteString(w, `"`)
})))
}
}

Expand Down Expand Up @@ -353,14 +353,13 @@ func (e *htmlElement) htmlWithIndent(w io.Writer, indent int) {
io.WriteString(w, k)

if v != "" {
io.WriteString(w, `="`)
io.WriteString(w, resolveAttributeURLValue(k, v, func(s string) string {
io.WriteString(w, `=`)
io.WriteString(w, strconv.Quote(resolveAttributeURLValue(k, v, func(s string) string {
if e.dispatcher != nil {
return e.dispatcher.resolveStaticResource(v)
}
return v
}))
io.WriteString(w, `"`)
})))
}
}

Expand Down
53 changes: 15 additions & 38 deletions pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -682,21 +681,11 @@ func (h *Handler) servePage(w http.ResponseWriter, r *http.Request) {
StaticResourceResolver: h.resolveStaticPath,
ActionHandlers: actionHandlers,
}
body := h.Body().privateBody(Div())
if err := mount(&disp, body); err != nil {
panic(errors.New("mounting pre-rendering container failed").
WithTag("server-side", disp.isServerSide()).
WithTag("body-type", reflect.TypeOf(disp.Body)).
Wrap(err))
}
disp.Body = body
disp.init()
defer disp.Close()

disp.Mount(Div().Body(
body := h.Body().privateBody(
Div(), // Pre-rendeging placeholder
Aside().
ID("app-wasm-loader").
Class("goapp-app-info").
Class("app-wasm-loader-hidden").
Body(
Img().
ID("app-wasm-loader-icon").
Expand All @@ -707,8 +696,18 @@ func (h *Handler) servePage(w http.ResponseWriter, r *http.Request) {
Class("goapp-label").
Text(page.loadingLabel),
),
Div().ID("app-pre-render").Body(content),
))
)
if err := mount(&disp, body); err != nil {
panic(errors.New("mounting pre-rendering container failed").
WithTag("server-side", disp.isServerSide()).
WithTag("body-type", reflect.TypeOf(disp.Body)).
Wrap(err))
}
disp.Body = body
disp.init()
defer disp.Close()

disp.Mount(content)

for len(disp.dispatches) != 0 {
disp.Consume()
Expand Down Expand Up @@ -865,13 +864,6 @@ type Icon struct {
// web app.
type Environment map[string]string

func normalizeFilePath(path string) string {
if runtime.GOOS == "windows" {
return strings.ReplaceAll(path, "/", `\`)
}
return path
}

func isRemoteLocation(path string) bool {
return strings.HasPrefix(path, "https://") ||
strings.HasPrefix(path, "http://")
Expand All @@ -881,18 +873,3 @@ func isStaticResourcePath(path string) bool {
return strings.HasPrefix(path, "/web/") ||
strings.HasPrefix(path, "web/")
}

type httpResource struct {
Path string
ContentType string
Body []byte
ExpireAt time.Time
}

func (r httpResource) Len() int {
return len(r.Body)
}

func (r httpResource) IsExpired() bool {
return r.ExpireAt != time.Time{} && r.ExpireAt.Before(time.Now())
}
Loading