Skip to content

Commit

Permalink
minor refactor vike-vue-query (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout authored Sep 12, 2024
1 parent 08b612f commit 46e12a7
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 161 deletions.
4 changes: 0 additions & 4 deletions examples/vue-pinia/layouts/LayoutDefault.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ a {
line-height: 1.8em;
border-right: 2px solid #eee;
}
.logo {
margin-top: 20px;
margin-bottom: 10px;
}
.content {
transition: opacity 0.1s ease-in;
}
Expand Down
51 changes: 31 additions & 20 deletions examples/vue-query/.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
import { test, expect, run, fetchHtml, page, getServerUrl } from '@brillout/test-e2e'
import { test, expect, run, fetchHtml, page, getServerUrl, autoRetry } from '@brillout/test-e2e'

runTest()

const fetchedText = 'A New Hope'
const url = '/'

function runTest() {
run('pnpm run dev')

const textLandingPage = 'A New Hope'
const title = 'Star Wars Movies'
testUrl({
url: '/',
title,
text: textLandingPage,
})
}

function testUrl({ url, title, text }: { url: string; title: string; text: string }) {
test(url + ' (HTML)', async () => {
test('HTML', async () => {
const html = await fetchHtml(url)
expect(html).toContain(text)
expect(getTitle(html)).toBe(title)
expect(html).toContain(fetchedText)
})
test(url + ' (Hydration)', async () => {

test('Hydration', async () => {
await page.goto(getServerUrl() + url)
const body = await page.textContent('body')
expect(body).toContain(text)
await testDOM()
})

test('Navigation', async () => {
await page.click('a[href="/about"]')
await page.click('a[href="/"]')
await testDOM()
})
}

async function testDOM() {
await testCounter()
const body = await page.textContent('body')
expect(body).toContain(fetchedText)
}

function getTitle(html: string) {
const title = html.match(/<title>(.*?)<\/title>/i)?.[1]
return title
async function testCounter() {
// autoRetry() for awaiting client-side code loading & executing
await autoRetry(
async () => {
expect(await page.textContent('button')).toBe('Counter 0')
await page.click('button')
expect(await page.textContent('button')).toContain('Counter 1')
},
{ timeout: 5 * 1000 },
)
}
36 changes: 0 additions & 36 deletions examples/vue-query/assets/logo.svg

This file was deleted.

8 changes: 0 additions & 8 deletions examples/vue-query/layouts/HeadDefault.vue

This file was deleted.

8 changes: 1 addition & 7 deletions examples/vue-query/layouts/LayoutDefault.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<template>
<div class="layout">
<div class="navigation">
<a href="/" class="logo">
<img src="../assets/logo.svg" height="64" width="64" />
</a>
<Link href="/">Welcome</Link>
<Link href="/about">About</Link>
</div>
<div class="content"><slot /></div>
</div>
Expand Down Expand Up @@ -47,10 +45,6 @@ a {
line-height: 1.8em;
border-right: 2px solid #eee;
}
.logo {
margin-top: 20px;
margin-bottom: 10px;
}
.content {
transition: opacity 0.1s ease-in;
}
Expand Down
10 changes: 4 additions & 6 deletions examples/vue-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
"@types/node-fetch": "^2.6.11",
"@tanstack/vue-query": "^5.0.0",
"node-fetch": "^3.3.2",
"typescript": "5.4.5",
"vike": "^0.4.195",
"vike-vue-query": "^0.1.2",
"vike-vue": "^0.8.4",
"vite": "^5.4.0",
"vue": "^3.4.36"
},
"type": "module",
"devDependencies": {
"typescript": "5.4.5",
"vue": "^3.4.36",
"vue-tsc": "^1.8.27"
}
},
"type": "module"
}
6 changes: 0 additions & 6 deletions examples/vue-query/pages/+config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import type { Config } from 'vike/types'
import Layout from '../layouts/LayoutDefault.vue'
import Head from '../layouts/HeadDefault.vue'
import vikeVue from 'vike-vue/config'
import vikeVueQuery from 'vike-vue-query/config'

// Default configs (can be overridden by pages)
export default {
Layout,
Head,
// <title>
title: 'My Vike + Vue + TanStack Query App',

extends: [vikeVue, vikeVueQuery],
} satisfies Config
4 changes: 2 additions & 2 deletions examples/vue-query/pages/+queryClientConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { queryClientConfig }

import type { QueryClientConfig } from '@tanstack/vue-query'

// set query client options - e.g. not refetching when window loses or gains focus during development
Expand All @@ -8,5 +10,3 @@ const queryClientConfig: QueryClientConfig = {
},
},
}

export default queryClientConfig
25 changes: 0 additions & 25 deletions examples/vue-query/pages/_error/+Page.vue

This file was deleted.

4 changes: 4 additions & 0 deletions examples/vue-query/pages/about/+Page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<h1>About</h1>
<p>Example of using TanStack Query.</p>
</template>
6 changes: 0 additions & 6 deletions examples/vue-query/pages/index/+Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
</template>
</ol>
<p>Source: <a href="https://brillout.github.io/star-wars">brillout.github.io/star-wars</a>.</p>
<p>
While initial data is fetched on the server, the client will refresh after rendering.<br>
<code>
Refreshing: {{ isFetching ? 'Yes' : 'No' }}
</code>
</p>
<p>
This page is:
</p>
Expand Down
1 change: 0 additions & 1 deletion examples/vue-query/pages/index/+title.ts

This file was deleted.

11 changes: 2 additions & 9 deletions examples/vue-query/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ import vue from '@vitejs/plugin-vue'
import vike from 'vike/plugin'
import { defineConfig } from 'vite'

const config = defineConfig({
plugins: [
vike({ prerender: true }),
vue({
include: [/\.vue$/, /\.md$/],
}),
],
export default defineConfig({
plugins: [vike(), vue()],
})

export default config
2 changes: 1 addition & 1 deletion packages/vike-vue-pinia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"vue-tsc": "^2.0.13"
},
"files": [
"dist"
"dist/"
],
"license": "MIT"
}
11 changes: 7 additions & 4 deletions packages/vike-vue-query/integration/+config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export { config as default }

import type { DehydratedState, QueryClient, QueryClientConfig } from '@tanstack/vue-query'
import type { Config } from 'vike/types'
import type _ from 'vike-vue/config' // Needed for declaration merging of Config

export default {
const config = {
name: 'vike-vue-query',
require: {
'vike-vue': '>=0.7.0',
},
onCreateApp: 'import:vike-vue-query/__internal/integration/installVueQuery:installVueQuery',
onAfterRenderHtml: 'import:vike-vue-query/__internal/integration/dehydrateVueQuery:dehydrateVueQuery',
onBeforeRenderClient: 'import:vike-vue-query/__internal/integration/hydrateVueQuery:hydrateVueQuery',
passToClient: ['_vueQueryInitialState'],
onCreateApp: 'import:vike-vue-query/__internal/integration/onCreateApp:onCreateApp',
onAfterRenderHtml: 'import:vike-vue-query/__internal/integration/onAfterRenderHtml:onAfterRenderHtml',
onBeforeRenderClient: 'import:vike-vue-query/__internal/integration/onBeforeRenderClient:onBeforeRenderClient',
meta: {
queryClientConfig: {
env: {
Expand Down
8 changes: 0 additions & 8 deletions packages/vike-vue-query/integration/dehydrateVueQuery.ts

This file was deleted.

13 changes: 13 additions & 0 deletions packages/vike-vue-query/integration/onAfterRenderHtml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export { onAfterRenderHtml }

import { dehydrate } from '@tanstack/vue-query'
import type { OnAfterRenderHtmlSync } from 'vike-vue/types'

const onAfterRenderHtml: OnAfterRenderHtmlSync = (pageContext): ReturnType<OnAfterRenderHtmlSync> => {
dehydrateVueQuery(pageContext)
}

type PageContext = Parameters<typeof onAfterRenderHtml>[0]
function dehydrateVueQuery(pageContext: PageContext) {
pageContext._vueQueryInitialState = dehydrate(pageContext.queryClient!)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export { onBeforeRenderClient }

import { hydrate } from '@tanstack/vue-query'
import type { OnBeforeRenderClientSync } from 'vike-vue/types'

export { hydrateVueQuery }
const onBeforeRenderClient: OnBeforeRenderClientSync = (pageContext): ReturnType<OnBeforeRenderClientSync> => {
hydrateVueQuery(pageContext)
}

const hydrateVueQuery: OnBeforeRenderClientSync = (pageContext): ReturnType<OnBeforeRenderClientSync> => {
type PageContext = Parameters<typeof onBeforeRenderClient>[0]
function hydrateVueQuery(pageContext: PageContext) {
if (!pageContext.isHydration) {
return
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export { onCreateApp }

import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
import type { OnCreateAppSync } from 'vike-vue/types'

export { installVueQuery }
const onCreateApp: OnCreateAppSync = (pageContext): ReturnType<OnCreateAppSync> => {
installVueQuery(pageContext)
}

const installVueQuery: OnCreateAppSync = (pageContext): ReturnType<OnCreateAppSync> => {
type PageContext = Parameters<typeof onCreateApp>[0]
function installVueQuery(pageContext: PageContext) {
const queryClient = new QueryClient(pageContext.config.queryClientConfig)
pageContext.app.use(VueQueryPlugin, { queryClient })
Object.assign(pageContext, { queryClient })
Expand Down
20 changes: 10 additions & 10 deletions packages/vike-vue-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
},
"exports": {
"./config": "./dist/+config.js",
"./__internal/integration/installVueQuery": "./dist/installVueQuery.js",
"./__internal/integration/dehydrateVueQuery": "./dist/dehydrateVueQuery.js",
"./__internal/integration/hydrateVueQuery": "./dist/hydrateVueQuery.js"
"./__internal/integration/onCreateApp": "./dist/onCreateApp.js",
"./__internal/integration/onAfterRenderHtml": "./dist/onAfterRenderHtml.js",
"./__internal/integration/onBeforeRenderClient": "./dist/onBeforeRenderClient.js"
},
"typesVersions": {
"*": {
"config": [
"./dist/+config.d.ts"
],
"__internal/integration/installVueQuery": [
"./dist/installVueQuery.d.ts"
"__internal/integration/onCreateApp": [
"./dist/onCreateApp.d.ts"
],
"__internal/integration/dehydrateVueQuery": [
"./dist/dehydrateVueQuery.d.ts"
"__internal/integration/onAfterRenderHtml": [
"./dist/onAfterRenderHtml.d.ts"
],
"__internal/integration/hydrateVueQuery": [
"./dist/hydrateVueQuery.d.ts"
"__internal/integration/onBeforeRenderClient": [
"./dist/onBeforeRenderClient.d.ts"
]
}
},
Expand All @@ -51,7 +51,7 @@
"vue-tsc": "^2.0.13"
},
"files": [
"dist"
"dist/"
],
"license": "MIT"
}
Loading

0 comments on commit 46e12a7

Please sign in to comment.