Skip to content

Instantly share code, notes, and snippets.

@stctheproducer
Last active November 15, 2024 12:05
Show Gist options
  • Save stctheproducer/a70772088a74fbbc4627540be122e40d to your computer and use it in GitHub Desktop.
Save stctheproducer/a70772088a74fbbc4627540be122e40d to your computer and use it in GitHub Desktop.
This is the Svelte implementation of the `Link` component provided by the `Tuyau` package (https://github.com/Julien-R44/tuyau). The types are a little hacky because the Inertia Link component was built in Svelte 4.
// ...imports here
import { setTuyauState, TUYAU_KEY } from '../components/tuyau_state'
const context = new Map([[TUYAU_KEY, setTuyauState()]])
createInertiaApp({
// ...other configs
setup({ el, App, props }) {
if (el?.dataset.serverRendered === 'true') {
hydrate(App, { target: el, props, context })
} else {
mount(App, { target: el!, props, context })
}
},
})
// ...imports here
import { TUYAU_KEY, setTuyauState } from '../components/tuyau_state'
export default function render(page: any) {
const context = new Map([[TUYAU_KEY, setTuyauState()]])
return createInertiaApp({
// ...other configs
setup({ App, props }) {
return renderSvelte(App, { props, context })
},
})
}
import { createTuyau } from '@tuyau/client'
import { api } from '#brisk/.adonisjs/api'
import { getContext } from 'svelte'
export const TUYAU_KEY = Symbol('tuyau')
export function setTuyauState() {
const tuyau = createTuyau({
api,
baseUrl: import.meta.env.VITE_APP_TUYAU_BASE_URL,
timeout: 10_000,
})
return tuyau
}
export function getTuyauState() {
const tuyau = getContext<ReturnType<typeof setTuyauState> | null>(TUYAU_KEY)
if (!tuyau) {
throw new Error('Tuyau client not found in context')
}
return tuyau
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment