-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make browser context creation implicit on the client (#843)
- Loading branch information
Showing
13 changed files
with
92 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@quilted/preact-browser': patch | ||
'@quilted/create': patch | ||
--- | ||
|
||
Make browser context creation implicit on the client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
import '@quilted/quilt/globals'; | ||
import {hydrate} from 'preact'; | ||
import {Browser, BrowserContext} from '@quilted/quilt/browser'; | ||
import {hydrate} from '@quilted/quilt/browser'; | ||
import {Router} from '@quilted/quilt/navigation'; | ||
|
||
import type {AppContext} from '~/shared/context.ts'; | ||
import {App} from './App.tsx'; | ||
|
||
const element = document.querySelector('#app')!; | ||
const browser = new Browser(); | ||
|
||
const context = { | ||
router: new Router(browser.request.url), | ||
router: new Router(), | ||
} satisfies AppContext; | ||
|
||
// Makes key parts of the app available in the browser console | ||
Object.assign(globalThis, {app: context}); | ||
|
||
hydrate( | ||
<BrowserContext browser={browser}> | ||
<App context={context} /> | ||
</BrowserContext>, | ||
element, | ||
); | ||
hydrate(<App context={context} />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,28 @@ | ||
import '@quilted/quilt/globals'; | ||
|
||
import {hydrate} from 'preact'; | ||
import {hydrate} from '@quilted/quilt/browser'; | ||
import {Router} from '@quilted/quilt/navigation'; | ||
|
||
import {httpBatchLink} from '@trpc/client'; | ||
import {QueryClient} from '@tanstack/react-query'; | ||
import {Browser, BrowserContext} from '@quilted/quilt/browser'; | ||
import {Router} from '@quilted/quilt/navigation'; | ||
|
||
import type {AppContext} from '~/shared/context.ts'; | ||
import {trpc} from '~/shared/trpc.ts'; | ||
|
||
import {App} from './App.tsx'; | ||
|
||
const element = document.querySelector('#app')!; | ||
const browser = new Browser(); | ||
|
||
const queryClient = new QueryClient(); | ||
const trpcClient = trpc.createClient({ | ||
links: [httpBatchLink({url: new URL('/api', window.location.href).href})], | ||
}); | ||
|
||
const context = { | ||
router: new Router(browser.request.url), | ||
router: new Router(), | ||
trpc: trpcClient, | ||
queryClient, | ||
} satisfies AppContext; | ||
|
||
// Makes key parts of the app available in the browser console | ||
Object.assign(globalThis, {app: context}); | ||
|
||
hydrate( | ||
<BrowserContext browser={browser}> | ||
<App context={context} /> | ||
</BrowserContext>, | ||
element, | ||
); | ||
hydrate(<App context={context} />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
hydrate as preactHydrate, | ||
render as preactRender, | ||
type ComponentChild, | ||
} from 'preact'; | ||
|
||
import {Browser} from '@quilted/browser'; | ||
|
||
import {BrowserDetailsContext} from './context.ts'; | ||
|
||
export function render( | ||
component: ComponentChild, | ||
{element}: {element?: string | Element} = {}, | ||
) { | ||
const browser = new Browser(); | ||
|
||
return preactRender( | ||
<BrowserDetailsContext.Provider value={browser}> | ||
{component} | ||
</BrowserDetailsContext.Provider>, | ||
resolveContainerNode(element), | ||
); | ||
} | ||
|
||
export function hydrate( | ||
component: ComponentChild, | ||
{element}: {element?: string | Element} = {}, | ||
) { | ||
const browser = new Browser(); | ||
|
||
return preactHydrate( | ||
<BrowserDetailsContext.Provider value={browser}> | ||
{component} | ||
</BrowserDetailsContext.Provider>, | ||
resolveContainerNode(element), | ||
); | ||
} | ||
|
||
function resolveContainerNode(selectorOrElement: string | Element = '#app') { | ||
if (typeof selectorOrElement !== 'string') return selectorOrElement; | ||
|
||
const element = document.querySelector(selectorOrElement); | ||
|
||
if (element == null) { | ||
throw new Error(`No element found for selector: ${selectorOrElement}`); | ||
} | ||
|
||
return element; | ||
} |
17 changes: 0 additions & 17 deletions
17
packages/preact-browser/source/components/BrowserContext.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from '@quilted/browser/testing'; | ||
export {BrowserContext} from './components/BrowserContext.tsx'; | ||
export {BrowserDetailsContext} from './context.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
import '@quilted/quilt/globals'; | ||
import {hydrate} from 'preact'; | ||
import {Browser, BrowserContext} from '@quilted/quilt/browser'; | ||
import {hydrate} from '@quilted/quilt/browser'; | ||
|
||
import App from './App.tsx'; | ||
|
||
const element = document.querySelector('#app')!; | ||
const browser = new Browser(); | ||
|
||
hydrate( | ||
<BrowserContext browser={browser}> | ||
<App /> | ||
</BrowserContext>, | ||
element, | ||
); | ||
hydrate(<App />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters