From 4baef11c2002c978bc357168854732454eddefda Mon Sep 17 00:00:00 2001 From: Hairok Date: Sun, 2 Jun 2024 08:40:24 +0200 Subject: [PATCH] Dead code elimination in Example Because Go compiler has dead code elimination, if we encapsulate the backend code in this if, as it is based on a constant value, the compiler will remove all code related to what's inside the if when compiling for wasm, leading to considerable size reduction. (In my playground app it reduces size from 21MB to 17MB uncompressed) --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 51079503..e16fe273 100644 --- a/README.md +++ b/README.md @@ -83,14 +83,16 @@ func main() { app.Route("/hello", func() app.Composer { return &hello{} }) app.RunWhenOnBrowser() - // HTTP routing: - http.Handle("/", &app.Handler{ - Name: "Hello", - Description: "An Hello World! example", - }) - - if err := http.ListenAndServe(":8000", nil); err != nil { - log.Fatal(err) + if app.IsServer { + // HTTP routing: + http.Handle("/", &app.Handler{ + Name: "Hello", + Description: "An Hello World! example", + }) + + if err := http.ListenAndServe(":8000", nil); err != nil { + log.Fatal(err) + } } } ```