thirdpartyjs

package
v4.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 3 Imported by: 0

README

Third-party JS integration (typed bridge + native parity)

A worked example of using a third-party JavaScript library from Go behind a typed bridge, with a pure-Go fallback so the same code runs on the server.

It wraps @sindresorhus/slugify:

bridge := thirdpartyjs.LoadSlugify(ctx)        // dynamic-imports the ESM module in the browser
slug := bridge.Slugify(ctx, "Hello, World!")   // → "hello-world"

The pattern

  1. Dynamic-import the module with interop.ImportModule(ctx, "https://esm.sh/...").
  2. Wrap it in a typed Go struct (SlugifyBridge) whose methods Call/CallDefault into the module and type-assert the results — callers see a normal Go API, not any.
  3. Fall back in Go. interop.ImportModule is a no-op stub on native/SSR (and a browser may be offline), so the bridge keeps a pure-Go equivalent. LoadSlugify returns an unloaded bridge that transparently routes to the fallback — callers never branch on platform.

Native-stub parity

slugify_test.go runs on the native lane, where ImportModule is unavailable: it asserts the bridge reports Loaded() == false yet still returns the correct slug via the Go fallback. That is the guarantee — the identical call site produces the identical result on the server and in the browser, so SSR and tests are never blocked by a browser-only dependency.

go test ./examples/public/third-party-js

Documentation

Overview

Package thirdpartyjs is a worked example of integrating a third-party JavaScript library behind a TYPED Go bridge using interop.ImportModule, with a pure-Go fallback so the exact same code runs on the server (SSR/native) where no JS module loader exists. It wraps the popular `@sindresorhus/slugify` ESM module: in the browser it calls the real library; everywhere else it transparently falls back to an equivalent Go implementation. This is the F5 pattern — dynamic-import a JS module, expose it as a typed Go API, and keep native-stub parity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlugifyBridge

type SlugifyBridge struct {
	// contains filtered or unexported fields
}

SlugifyBridge is a typed wrapper over the imported JS module. The zero value is usable: it behaves as "module not loaded" and routes through the Go fallback.

func LoadSlugify

func LoadSlugify(parseCtx context.Context) SlugifyBridge

LoadSlugify dynamically imports the slugify module and returns a bridge bound to it. When the import is unavailable (native/server, or an offline browser), it returns an UNLOADED bridge that still works via the Go fallback — so callers never have to special-case the platform.

func (SlugifyBridge) Dispose

func (parseB SlugifyBridge) Dispose() error

Dispose releases the imported module's resources, if any. Safe to call on an unloaded bridge.

func (SlugifyBridge) Loaded

func (parseB SlugifyBridge) Loaded() bool

Loaded reports whether the real JS module backs this bridge (false → the Go fallback is used).

func (SlugifyBridge) Slugify

func (parseB SlugifyBridge) Slugify(parseCtx context.Context, parseText string) string

Slugify converts text to a URL slug. When the JS module is loaded it calls the library's default export; otherwise — and whenever the call fails or returns an unexpected type — it falls back to the equivalent Go implementation, guaranteeing the same well-formed slug on every platform.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL