Skip to content

Views

Views are svelte components exported by app/exports.server.ts and/or app/exports.client.ts.

  • Directoryapp
    • exports.client.ts
    • exports.server.ts

Views that are meant to be rendered on the server should be exported by app/exports.server.ts.

app/exports.server.ts
import Welcome from '$lib/views/Welcome.svelte'
import Profile from '$lib/views/Profile.svelte'
export const views = {
"Welcome": Welcome,
"Profile": Profile,
}

Views that are meant to be rendered on the client should be exported by app/exports.client.ts.

app/exports.client.ts
export const views = {
"Welcome": () => import('$lib/views/Welcome.svelte'),
"Profile": () => import('$lib/views/Profile.svelte'),
}

Use send.View() to send a view.

lib/routes/welcome/view.go
package welcome
import (
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
)
func View(client *clients.Client) {
send.View(client, views.View{Name: "Welcome"}) // Sends view "Welcome".
}

The Name of the view will be used to lookup the view component exported by app/exports.server.ts and/or app/exports.client.ts.

There is no way to specify a “default view”.

However, you can use send.RequestedFile() in order to send the requested file or run custom logic if it doesn’t exist.

lib/routes/welcome/view.go
package welcome
import (
"os"
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
"main/lib/routes/welcome"
)
func View(client *clients.Client) {
if !send.RequestedFile() {
welcome.View(client)
}
}

Usually you would map this handler to the default GET / pattern, which automatically captures all unmatched requests.

package main
import (
"embed"
"main/lib/core/clients"
"main/lib/core/servers"
"main/lib/routes/welcome"
)
//go:embed app/dist
var efs embed.FS
var server = servers.New() // Creates server.
func main() {
defer servers.Start(server) // Starts server.
server.Efs = efs // Sets embedded file system.
server.Routes = append(server.Routes, routes.Route{ // Adds route to the server.
Pattern: "GET /",
Handler: welcome.View,
})
}

Optionally, you can specify properties for your View with the Props field.

lib/routes/welcome/view.go
package welcome
import (
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
)
func View(client *clients.Client) {
send.View(client, views.View{ // Sends view.
Name: "Welcome", // Sets view name.
Props: map[string]string{ // Sets view props, which will be injected into the svelte component.
"name": "world", // Adds property "name" with value "world".
},
})
}

These properties are passed down to your view component.

app/lib/views/Hello.svelte
<script lang="ts">
type Props = { name: string }
let {name}:Props = $props() // Retrieves view props.
</script>
<h1>Hello {name}</h1>

You can choose how to render views by setting the RenderMode field in your View.

Using RenderModeFull, the view is rendered on both the server and the client.
This is the default mode.

lib/routes/welcome/view.go
package welcome
import (
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
)
func View(client *clients.Client) {
send.View(client, views.View{ // Sends view.
Name: "Welcome", // Sets view name.
RenderMode: view.RenderModeFull, // Renders view on server and client.
})
}

Using RenderModeServer, the view is rendered only on the server.
You’ll have to deal away with apis such as fetch; your new best friend is form.

lib/routes/welcome/view.go
package welcome
import (
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
)
func View(client *clients.Client) {
send.View(client, views.View{ // Sends view.
Name: "Welcome", // Sets view name.
RenderMode: view.RenderModeServer, // Renders view only on server.
})
}

Using RenderModeClient, the view is rendered only on the client by loading a JavaScript bundle asynchronously.

lib/routes/welcome/view.go
package welcome
import (
"main/lib/core/clients"
"main/lib/core/send"
"main/lib/core/views"
)
func View(client *clients.Client) {
send.View(client, views.View{ // Sends view.
Name: "Welcome", // Sets view name.
RenderMode: view.RenderModeClient, // Renders view only on client.
})
}

Disabling the server-side JavaScript runtime.

Section titled “Disabling the server-side JavaScript runtime.”

You can add the no_js_runtime tag to your build process to completely disable the server-side JavaScript runtime.

Terminal window
frizzante --build --tags="no_js_runtime"

This will reduce the minimum size of the final binary from 25MB to 10MB.