Skip to content

Commit

Permalink
No wasm mode (#810)
Browse files Browse the repository at this point in the history
* query parameter to see a page without wasm

* pre render at same depth as render

* remove loader node rather than hide it

* Update html.go

* Update html.go

* prevent loader to show in non wasm mode

* cleanup
  • Loading branch information
maxence-charriere authored Feb 8, 2023
1 parent 1b96c5b commit a313eaa
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 53 deletions.
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

0 comments on commit a313eaa

Please sign in to comment.