Skip to content

Superglue

Classes

SuperglueResponseError

Defined in: utils/request.ts:26

Extends

  • Error

Constructors

Constructor

new SuperglueResponseError(message: string): SuperglueResponseError

Defined in: utils/request.ts:29

Parameters
Parameter Type
message string
Returns

SuperglueResponseError

Overrides

Error.constructor

Properties

Property Type Defined in
response Response utils/request.ts:27

Interfaces

ChannelMixin

Defined in: types/cable.ts:25

Mixin passed to subscriptions.create. Mirrors the subset of the ActionCable subscription mixin that Superglue invokes.

Methods

received()

received(message: string): void

Defined in: types/cable.ts:26

Parameters
Parameter Type
message string
Returns

void

connected()

connected(): void

Defined in: types/cable.ts:27

Returns

void

disconnected()

disconnected(): void

Defined in: types/cable.ts:28

Returns

void


Subscription

Defined in: types/cable.ts:35

The handle returned from subscriptions.create.

Methods

unsubscribe()

unsubscribe(): void

Defined in: types/cable.ts:39

Returns

void


Subscriptions

Defined in: types/cable.ts:46

The subscriptions namespace on a Consumer.

Methods

create()

create(channel: string | ChannelNameWithParams, mixin: ChannelMixin): Subscription

Defined in: types/cable.ts:47

Parameters
Parameter Type
channel string | ChannelNameWithParams
mixin ChannelMixin
Returns

Subscription


Consumer

Defined in: types/cable.ts:59

The minimal Consumer surface Superglue depends on. Pass an instance of this (e.g. createConsumer('/cable') from @rails/actioncable or createCable() from @anycable/web) as cable to createApp.

Properties

Property Type Defined in
subscriptions Subscriptions types/cable.ts:60

Type Aliases

ChannelNameWithParams

ChannelNameWithParams = {[key: string]: undefined | string | number | boolean; channel: string; }

Defined in: types/cable.ts:15

A channel identifier. Either a bare channel name string or an object with a channel key plus arbitrary identifier params.

Indexable

[key: string]: undefined | string | number | boolean

Properties

channel

channel: string

Defined in: types/cable.ts:16

Functions

webVisit()

webVisit(store: SuperglueStore, path: string, options?: VisitProps): Promise\<VisitResult | ErrorResult>

Defined in: action_creators/web.ts:18

Web specific wrapper around the visit thunk adding asset-refresh when the response indicates the client bundle is stale.

Returns a never-settling promise on that branch so the chain terminates with the browser unload.

Parameters

Parameter Type
store SuperglueStore
path string
options? VisitProps

Returns

Promise\<VisitResult | ErrorResult>


webRemote()

webRemote(store: SuperglueStore, path: string, options?: RemoteProps): Promise\<Result | ErrorResult>

Defined in: action_creators/web.ts:39

Web specific wrapper around the remote thunk. Just a passthrough.

Parameters

Parameter Type
store SuperglueStore
path string
options? RemoteProps

Returns

Promise\<Result | ErrorResult>


toFragmentRef()

toFragmentRef\<T, P>(id: string): FragmentRef\<T, P>

Defined in: hooks/useFragment.tsx:14

Converts a string ID to a typed FragmentRef.

Type Parameters

Type Parameter Default type
T -
P extends boolean false

Parameters

Parameter Type
id string

Returns

FragmentRef\<T, P>

Example

const author = useFragment(toFragmentRef<Author>("author_123"))

useFragment()

useFragment\<T, P>(fragmentRef: FragmentRef\<T, P>, options?: ValidateOption): P extends true ? T : undefined | T

Defined in: hooks/useFragment.tsx:62

Returns a proxy for accessing a fragment's content from the store.

Passing in a fragment reference scopes the tracking of fragments to that hook usage. This is useful in performance scenarios where you want a child component to update, but not the parent.

import {unproxy} from '@thoughtbot/superglue'

const content = useContent()
const rawContent = unproxy(content)

<h1>{content.title}</h1>
<SlidingCart cartRef={rawContent.cart} />

then in SlidingCart

const SlidingCart = (cartRef) => {
  const cart = useFragment(cartRef)
}

SlidingCart will update only if the fragment referenced by cartRef updates.

Type Parameters

Type Parameter Description
T The data type being accessed
P extends boolean Whether the fragment is guaranteed to be present

Parameters

Parameter Type Description
fragmentRef FragmentRef\<T, P> A typed fragment reference
options? ValidateOption -

Returns

P extends true ? T : undefined | T

Reactive proxy to fragment data. Returns T if present, T | undefined otherwise.

Example

// Access fragment via unproxied ref
const rawContent = unproxy(content)
const user = useFragment(rawContent.user)

// Access fragment via string ID
const cart = useFragment(toFragmentRef<Cart>("cart_123"))

useUpdateContent()

useUpdateContent(): \<T>(pageKey: string, updater: (draft: T) => void) => void

Defined in: hooks/useUpdateContent.tsx:9

Returns

\<T>(pageKey: string, updater: (draft: T) => void): void

Type Parameters
Type Parameter Default type
T unknown
Parameters
Parameter Type
pageKey string
updater (draft: T) => void
Returns

void


createApp()

createApp(__namedParameters: CreateAppArgs & { _storeResult?: StoreResult; }): CreateAppResult

Defined in: index.tsx:80

Bootstrap a Superglue application. Performs all of the per-request setup (resetting the store, building visit/remote, installing history, seeding the initial page) and returns a { Provider, Outlet } pair the caller can arrange in their own React tree.

const { Provider, Outlet, ujs } = createApp({
  initialPage: window.SUPERGLUE_INITIAL_PAGE_STATE,
  baseUrl: location.origin,
  path: location.pathname + location.search + location.hash,
  mapping: pageIdentifierToPageComponent,
  buildVisitAndRemote,
})

createRoot(el).render(
  <div onClick={ujs.onClick} onSubmit={ujs.onSubmit}>
    <Provider>
      <AppLayout>
        <Outlet />
      </AppLayout>
    </Provider>
  </div>
)

Parameters

Parameter Type
__namedParameters CreateAppArgs & { _storeResult?: StoreResult; }

Returns

CreateAppResult


getIn()

getIn(node: JSONMappable, path: string): JSONValue

Defined in: utils/immutability.ts:22

Retrieves data from a JSON object using a Keypath

Parameters

Parameter Type Description
node JSONMappable
path string

Returns

JSONValue


urlToPageKey()

urlToPageKey(url: string): string

Defined in: utils/url.ts:49

Converts a url to a PageKey.

Parameters

Parameter Type Description
url string

Returns

string

References

useContent

Re-exports useContent


useUpdateFragment

Re-exports useUpdateFragment


useStreamSource

Re-exports useStreamSource


useSuperglue

Re-exports useSuperglue


useFlash

Re-exports useFlash


useSetFlash

Re-exports useSetFlash


Re-exports NavigationProvider


Re-exports NavigationOutlet


Re-exports NavigationContext


unproxy

Re-exports unproxy


FetchArgs

Re-exports FetchArgs


GraftingSuccessAction

Re-exports GraftingSuccessAction


GraftingErrorAction

Re-exports GraftingErrorAction


ValidateOption

Re-exports ValidateOption


PageKey

Re-exports PageKey


RestoreStrategy

Re-exports RestoreStrategy


Re-exports NavigationAction


ComponentIdentifier

Re-exports ComponentIdentifier


Keypath

Re-exports Keypath


JSONPrimitive

Re-exports JSONPrimitive


JSONObject

Re-exports JSONObject


JSONMappable

Re-exports JSONMappable


JSONKeyable

Re-exports JSONKeyable


JSONValue

Re-exports JSONValue


FlashState

Re-exports FlashState


Fragment

Re-exports Fragment


Unproxy

Re-exports Unproxy


ParsedResponse

Re-exports ParsedResponse


Defer

Re-exports Defer


SaveResponse

Re-exports SaveResponse


Page

Re-exports Page


GraftResponse

Re-exports GraftResponse


StreamMessage

Re-exports StreamMessage


StreamResponse

Re-exports StreamResponse


PageResponse

Re-exports PageResponse


FragmentPath

Re-exports FragmentPath


FragmentRef

Re-exports FragmentRef


AllPages

Re-exports AllPages


AllFragments

Re-exports AllFragments


SuperglueState

Re-exports SuperglueState


RootState

Re-exports RootState


Result

Re-exports Result


VisitResult

Re-exports VisitResult


ErrorResult

Re-exports ErrorResult


VisitCreator

Re-exports VisitCreator


RemoteCreator

Re-exports RemoteCreator


Dispatch

Re-exports Dispatch


SuperglueStore

Re-exports SuperglueStore


Handlers

Re-exports Handlers


UJSHandlers

Re-exports UJSHandlers


HistoryState

Re-exports HistoryState


SaveAndProcessPageThunk

Re-exports SaveAndProcessPageThunk


MetaThunk

Re-exports MetaThunk


VisitMetaThunk

Re-exports VisitMetaThunk


DefermentThunk

Re-exports DefermentThunk


BasicRequestInit

Re-exports BasicRequestInit


Re-exports NavigateTo


CopyTo

Re-exports CopyTo


Re-exports NavigationContextProps


Re-exports NavigationProviderProps


Re-exports NavigationOutletProps


BuildStore

Re-exports BuildStore


BuildVisitAndRemote

Re-exports BuildVisitAndRemote


BuildVisitAndRemoteContext

Re-exports BuildVisitAndRemoteContext


CreateAppArgs

Re-exports CreateAppArgs


ProviderProps

Re-exports ProviderProps


CreateAppResult

Re-exports CreateAppResult


Visit

Re-exports Visit


VisitProps

Re-exports VisitProps


Remote

Re-exports Remote


RemoteProps

Re-exports RemoteProps


BeforeSave

Re-exports BeforeSave


ApplicationRemote

Re-exports ApplicationRemote


ApplicationVisit

Re-exports ApplicationVisit