mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 00:59:34 +08:00

* New translations minecraftupdater.mdx (Korean) [ci skip] * New translations links.mdx (Korean) [ci skip] * New translations templates.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations developing-new-features.mdx (Korean) [ci skip] * New translations documenting.mdx (Korean) [ci skip] * New translations troubleshooting.mdx (Korean) [ci skip] * New translations windows-installer.mdx (Korean) [ci skip] * New translations faq.mdx (Korean) [ci skip] * New translations 2021-11-08-v2-beta2-release-notes.mdx (Korean) [ci skip] * New translations 2022-02-22-v2-beta3-release-notes.mdx (Korean) [ci skip] * New translations authors.yml (Korean) [ci skip] * New translations credits.mdx (Korean) [ci skip] * New translations markdown-page.md (Korean) [ci skip] * New translations stats.mdx (Korean) [ci skip] * New translations faq.mdx (Japanese) [ci skip] * New translations faq.mdx (Russian) [ci skip] * New translations introduction.mdx (Korean) [ci skip] * New translations faq.mdx (Chinese Simplified) [ci skip] * New translations options.json (Korean) [ci skip] * New translations current.json (Korean) [ci skip] * New translations version-v2.0.0-beta.38.json (Korean) [ci skip] * New translations version-v2.0.0-beta.39.json (Korean) [ci skip] * New translations footer.json (Korean) [ci skip] * New translations navbar.json (Korean) [ci skip] * New translations 2021-09-27-v2-beta1-release-notes.mdx (Korean) [ci skip] * New translations howdoesitwork.mdx (Korean) [ci skip] * New translations windows.mdx (Korean) [ci skip] * New translations log.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations linux.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations browser.mdx (Korean) [ci skip] * New translations dialog.mdx (Korean) [ci skip] * New translations events.mdx (Korean) [ci skip] * New translations intro.mdx (Korean) [ci skip] * New translations menu.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations window.mdx (Korean) [ci skip] * New translations cli.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations menus.mdx (Korean) [ci skip] * New translations options.mdx (Korean) [ci skip] * New translations project-config.mdx (Korean) [ci skip] * New translations _category_.json (Korean) [ci skip] * New translations helloworld.mdx (Korean) [ci skip] * New translations code.json (Korean) [ci skip] * New translations developing-new-features.mdx (Chinese Simplified) [ci skip] * New translations firstproject.mdx (Japanese) [ci skip] * New translations firstproject.mdx (Russian) [ci skip] * New translations firstproject.mdx (Chinese Simplified) [ci skip] * New translations installation.mdx (Japanese) [ci skip] * New translations installation.mdx (Russian) [ci skip] * New translations installation.mdx (Chinese Simplified) [ci skip] * New translations dynamic-assets.mdx (Japanese) [ci skip] * New translations dynamic-assets.mdx (Russian) [ci skip] * New translations dynamic-assets.mdx (Chinese Simplified) [ci skip] * New translations frameless.mdx (Japanese) [ci skip] * New translations frameless.mdx (Russian) [ci skip] * New translations frameless.mdx (Chinese Simplified) [ci skip] * New translations documenting.mdx (Japanese) [ci skip] * New translations documenting.mdx (Russian) [ci skip] * New translations frontend.mdx (Japanese) [ci skip] * New translations frontend.mdx (Russian) [ci skip] * New translations windows.mdx (Chinese Simplified) [ci skip] * New translations troubleshooting.mdx (Japanese) [ci skip] * New translations troubleshooting.mdx (Russian) [ci skip] * New translations troubleshooting.mdx (Chinese Simplified) [ci skip] * New translations windows-installer.mdx (Japanese) [ci skip] * New translations windows-installer.mdx (Russian) [ci skip] * New translations windows-installer.mdx (Chinese Simplified) [ci skip] * New translations dialog.mdx (Japanese) [ci skip] * New translations dialog.mdx (Russian) [ci skip] * New translations signing.mdx (Chinese Simplified) [ci skip] * New translations migrating.mdx (Russian) [ci skip] * New translations ides.mdx (Japanese) [ci skip] * New translations ides.mdx (Russian) [ci skip] * New translations ides.mdx (Chinese Simplified) [ci skip] * New translations migrating.mdx (Japanese) [ci skip] * New translations migrating.mdx (Chinese Simplified) [ci skip] * New translations signing.mdx (Russian) [ci skip] * New translations mouse-buttons.mdx (Japanese) [ci skip] * New translations mouse-buttons.mdx (Russian) [ci skip] * New translations mouse-buttons.mdx (Chinese Simplified) [ci skip] * New translations overscroll.mdx (Japanese) [ci skip] * New translations overscroll.mdx (Russian) [ci skip] * New translations routing.mdx (Japanese) [ci skip] * New translations routing.mdx (Russian) [ci skip] * New translations routing.mdx (Chinese Simplified) [ci skip] * New translations signing.mdx (Japanese) [ci skip] * New translations dialog.mdx (Chinese Simplified) [ci skip] * New translations project-config.mdx (Chinese Simplified) [ci skip] * New translations options.mdx (Chinese Simplified) [ci skip] * New translations howdoesitwork.mdx (Japanese) [ci skip] * New translations howdoesitwork.mdx (Russian) [ci skip] * New translations howdoesitwork.mdx (Chinese Simplified) [ci skip] * New translations 2021-09-27-v2-beta1-release-notes.mdx (Japanese) [ci skip] * New translations 2021-09-27-v2-beta1-release-notes.mdx (Russian) [ci skip] * New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified) [ci skip] * New translations 2021-11-08-v2-beta2-release-notes.mdx (Japanese) [ci skip] * New translations options.mdx (Russian) [ci skip] * New translations window.mdx (Japanese) [ci skip] * New translations menu.mdx (Japanese) [ci skip] * New translations menu.mdx (Russian) [ci skip] * New translations menu.mdx (Chinese Simplified) [ci skip] * New translations window.mdx (Russian) [ci skip] * New translations options.mdx (Japanese) [ci skip] * New translations cli.mdx (Chinese Simplified) [ci skip] * New translations menus.mdx (Japanese) [ci skip] * New translations menus.mdx (Russian) [ci skip] * New translations menus.mdx (Chinese Simplified) [ci skip] * New translations 2021-11-08-v2-beta2-release-notes.mdx (Russian) [ci skip] * New translations 2021-11-08-v2-beta2-release-notes.mdx (Chinese Simplified) [ci skip] * New translations emailit.mdx (Chinese Simplified) [ci skip] * New translations modalfilemanager.mdx (Chinese Simplified) [ci skip] * New translations 2022-02-22-v2-beta3-release-notes.mdx (Japanese) [ci skip] * New translations 2022-02-22-v2-beta3-release-notes.mdx (Russian) [ci skip] * New translations 2022-02-22-v2-beta3-release-notes.mdx (Chinese Simplified) [ci skip] * New translations credits.mdx (Japanese) [ci skip] * New translations credits.mdx (Russian) [ci skip] * New translations credits.mdx (Chinese Simplified) [ci skip] * New translations code.json (Korean) [ci skip] * New translations developing-new-features.mdx (Japanese) [ci skip] * New translations developing-new-features.mdx (Russian) [ci skip] * New translations ways-of-contributing.mdx (Japanese) [ci skip] * New translations documenting.mdx (Japanese) [ci skip] * New translations testing.mdx (Japanese) [ci skip] * New translations setting-up-a-dev-environment.mdx (Japanese) [ci skip] * New translations fixing-bugs.mdx (Japanese) [ci skip] * New translations helping-others.mdx (Japanese) [ci skip] * New translations ways-of-contributing.mdx (Russian) [ci skip] * New translations fixing-bugs.mdx (Chinese Simplified) [ci skip] * New translations documenting.mdx (Russian) [ci skip] * New translations documenting.mdx (Chinese Simplified) [ci skip] * New translations testing.mdx (Chinese Simplified) [ci skip] * New translations fixing-bugs.mdx (Russian) [ci skip] * New translations ways-of-contributing.mdx (Chinese Simplified) [ci skip] * New translations testing.mdx (Russian) [ci skip] * New translations helping-others.mdx (Russian) [ci skip] * New translations helping-others.mdx (Chinese Simplified) [ci skip] * New translations setting-up-a-dev-environment.mdx (Chinese Simplified) [ci skip] * New translations setting-up-a-dev-environment.mdx (Russian) [ci skip] * New translations developing-new-features.mdx (Chinese Simplified) [ci skip] * New translations current.json (Japanese) [ci skip] * New translations current.json (Russian) [ci skip] * New translations current.json (Chinese Simplified) [ci skip] * New translations version-v2.0.0-beta.44.json (Japanese) [ci skip] * New translations version-v2.0.0-beta.44.json (Russian) [ci skip] * New translations version-v2.0.0-beta.44.json (Chinese Simplified) [ci skip] * New translations version-v2.0.0-beta.43.json (Japanese) [ci skip] * New translations version-v2.0.0-beta.43.json (Russian) [ci skip] * New translations version-v2.0.0-beta.43.json (Chinese Simplified) [ci skip] * New translations installation.mdx (Korean) [ci skip] * New translations ways-of-contributing.mdx (Korean) [ci skip] * New translations testing.mdx (Korean) [ci skip] * New translations developing-new-features.mdx (Korean) [ci skip] * New translations documenting.mdx (Korean) [ci skip] * New translations fixing-bugs.mdx (Korean) [ci skip] * New translations helping-others.mdx (Korean) [ci skip] * New translations setting-up-a-dev-environment.mdx (Korean) [ci skip] * New translations current.json (Korean) [ci skip] * New translations options.json (Korean) [ci skip] * New translations version-v2.0.0-beta.44.json (Korean) [ci skip] * New translations version-v2.0.0-beta.43.json (Korean) [ci skip] * New translations footer.json (Korean) [ci skip] * New translations navbar.json (Korean) [ci skip] * New translations code.json (Korean) [ci skip] * New translations community-guide.mdx (Japanese) [ci skip] * New translations community-guide.mdx (Korean) [ci skip] * New translations community-guide.mdx (Russian) [ci skip] * New translations community-guide.mdx (Chinese Simplified) [ci skip] * New translations current.json (Japanese) [ci skip] * New translations current.json (Russian) [ci skip] * New translations current.json (Chinese Simplified) [ci skip] * New translations code.json (Japanese) [ci skip] * New translations code.json (Russian) [ci skip] * New translations code.json (Chinese Simplified) [ci skip] * New translations introduction.mdx (Korean) [ci skip] * New translations current.json (Korean) [ci skip] * New translations code.json (Korean) [ci skip] * New translations community-guide.mdx (Japanese) [ci skip] * New translations community-guide.mdx (Korean) [ci skip] * New translations community-guide.mdx (Russian) [ci skip] * New translations community-guide.mdx (Chinese Simplified) [ci skip] * New translations community-guide.mdx (Chinese Simplified) [ci skip] * New translations vscode.mdx (Japanese) [ci skip] * New translations vscode.mdx (Korean) [ci skip] * New translations vscode.mdx (Russian) [ci skip] * New translations vscode.mdx (Chinese Simplified) [ci skip] * New translations current.json (Chinese Simplified) [ci skip] * New translations riftshare.mdx (Chinese Simplified) [ci skip] * New translations surge.mdx (Chinese Simplified) [ci skip] * New translations wally.mdx (Chinese Simplified) [ci skip] * New translations encrypteasy.mdx (Chinese Simplified) [ci skip] * New translations filehound.mdx (Chinese Simplified) [ci skip] * New translations mollywallet.mdx (Chinese Simplified) [ci skip] * New translations optimus.mdx (Chinese Simplified) [ci skip] * New translations _category_.json (Chinese Simplified) [ci skip] * New translations emailit.mdx (Chinese Simplified) [ci skip] * New translations modalfilemanager.mdx (Chinese Simplified) [ci skip] * New translations browser.mdx (Chinese Simplified) [ci skip] * New translations vscode.mdx (Russian) [ci skip] * New translations vscode.mdx (Chinese Simplified) [ci skip] * New translations browser.mdx (Japanese) [ci skip] * New translations browser.mdx (Korean) [ci skip] * New translations browser.mdx (Russian) [ci skip] * New translations dialog.mdx (Japanese) [ci skip] * New translations vscode.mdx (Japanese) [ci skip] * New translations dialog.mdx (Korean) [ci skip] * New translations dialog.mdx (Russian) [ci skip] * New translations dialog.mdx (Chinese Simplified) [ci skip] * New translations events.mdx (Japanese) [ci skip] * New translations events.mdx (Korean) [ci skip] * New translations events.mdx (Russian) [ci skip] * New translations events.mdx (Chinese Simplified) [ci skip] * New translations intro.mdx (Japanese) [ci skip] * New translations intro.mdx (Korean) [ci skip] * New translations intro.mdx (Russian) [ci skip] * New translations vscode.mdx (Korean) [ci skip] * New translations intro.mdx (Chinese Simplified) [ci skip] * New translations log.mdx (Japanese) [ci skip] * New translations options.mdx (Russian) [ci skip] * New translations introduction.mdx (Japanese) [ci skip] * New translations options.mdx (Chinese Simplified) [ci skip] * New translations options.mdx (Korean) [ci skip] * New translations log.mdx (Korean) [ci skip] * New translations log.mdx (Russian) [ci skip] * New translations log.mdx (Chinese Simplified) [ci skip] * New translations menu.mdx (Japanese) [ci skip] * New translations menu.mdx (Korean) [ci skip] * New translations menu.mdx (Russian) [ci skip] * New translations menu.mdx (Chinese Simplified) [ci skip] * New translations window.mdx (Japanese) [ci skip] * New translations window.mdx (Korean) [ci skip] * New translations window.mdx (Russian) [ci skip] * New translations window.mdx (Chinese Simplified) [ci skip] * New translations options.mdx (Japanese) [ci skip] * New translations introduction.mdx (Korean) [ci skip] * New translations introduction.mdx (Russian) [ci skip] * New translations community-guide.mdx (Chinese Simplified) [ci skip] * New translations stats.mdx (Chinese Simplified) [ci skip] * New translations faq.mdx (Chinese Simplified) [ci skip] * New translations markdown-page.md (Chinese Simplified) [ci skip] * New translations introduction.mdx (Chinese Simplified) [ci skip] * New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified) [ci skip] * New translations version-v2.0.0-beta.44.json (Chinese Simplified) [ci skip] * Empty-Commit * Fix crowdin screwups again
127 lines
3.5 KiB
Plaintext
127 lines
3.5 KiB
Plaintext
# Dynamic Assets
|
|
|
|
If you want to load or generate assets for your frontend dynamically, you can achieve that using the [AssetsHandler](../reference/options#assetshandler) option. The AssetsHandler is a generic `http.Handler` which will be called for any non GET request on the assets server and for GET requests which can not be served from the bundled assets because the file is not found.
|
|
|
|
By installing a custom AssetsHandler, you can serve your own assets using a custom asset server.
|
|
|
|
## Example
|
|
|
|
In our example project, we will create a simple assets handler which will load files off disk:
|
|
|
|
```go title=main.go {16-35,49}
|
|
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"github.com/wailsapp/wails/v2"
|
|
"github.com/wailsapp/wails/v2/pkg/options"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed frontend/dist
|
|
var assets embed.FS
|
|
|
|
type FileLoader struct {
|
|
http.Handler
|
|
}
|
|
|
|
func NewFileLoader() *FileLoader {
|
|
return &FileLoader{}
|
|
}
|
|
|
|
func (h *FileLoader) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|
var err error
|
|
requestedFilename := strings.TrimPrefix(req.URL.Path, "/")
|
|
println("Requesting file:", requestedFilename)
|
|
fileData, err := os.ReadFile(requestedFilename)
|
|
if err != nil {
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
res.Write([]byte(fmt.Sprintf("Could not load file %s", requestedFilename)))
|
|
}
|
|
|
|
res.Write(fileData)
|
|
}
|
|
|
|
func main() {
|
|
// Create an instance of the app structure
|
|
app := NewApp()
|
|
|
|
// Create application with options
|
|
err := wails.Run(&options.App{
|
|
Title: "helloworld",
|
|
Width: 1024,
|
|
Height: 768,
|
|
Assets: assets,
|
|
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 255},
|
|
OnStartup: app.startup,
|
|
AssetsHandler: NewFileLoader(),
|
|
Bind: []interface{}{
|
|
app,
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
println("Error:", err)
|
|
}
|
|
}
|
|
```
|
|
|
|
When we run the application in dev mode using `wails dev`, we will see the following output:
|
|
|
|
```
|
|
DEB | [ExternalAssetHandler] Loading 'http://localhost:3001/favicon.ico'
|
|
DEB | [ExternalAssetHandler] Loading 'http://localhost:3001/favicon.ico' failed, using AssetHandler
|
|
Requesting file: favicon.ico
|
|
```
|
|
|
|
As you can see, the assets handler is called when the default assets server is unable to serve the `favicon.ico` file.
|
|
|
|
If you right click the main application and select "inspect" to bring up the devtools, you can test this feature out by typing the following into the console:
|
|
|
|
```
|
|
let response = await fetch('does-not-exist.txt');
|
|
```
|
|
|
|
This will generate an error in the devtools. We can see that the error is what we expect, returned by our custom assets handler:
|
|
|
|
<p className="text--center">
|
|
<img
|
|
src={require("@site/static/img/assetshandler-does-not-exist.png").default}
|
|
/>
|
|
</p>
|
|
|
|
However, if we request `go.mod`, we will see the following output:
|
|
|
|
<p className="text--center">
|
|
<img src={require("@site/static/img/assetshandler-go-mod.png").default} />
|
|
</p>
|
|
|
|
This technique can be used to load images directly into the page. If we updated our default vanilla template and replaced the logo image:
|
|
|
|
```html
|
|
<img id="logo" class="logo">
|
|
```
|
|
|
|
with:
|
|
|
|
```html
|
|
<img src="build/appicon.png" style="width: 300px">
|
|
```
|
|
|
|
Then we would see the following:
|
|
|
|
<p className="text--center">
|
|
<img
|
|
src={require("@site/static/img/assetshandler-image.png").default}
|
|
style={{ width: "75%" }}
|
|
/>
|
|
</p>
|
|
|
|
:::warning
|
|
Exposing your filesystem in this way is a security risk. It is recommended that you properly manage access
|
|
to your filesystem.
|
|
:::
|