Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat server fn public paths #3055

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pnpm-lock.yaml
node_modules

**/test-results
**/tests/generator/file-modification/routes/(test)/*
**/tests/generator/file-modification/routes/(test)/*
/.nx/workspace-data
4 changes: 2 additions & 2 deletions examples/react/start-basic/app/routes/deferred.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { createServerFn } from '@tanstack/start'
import { Suspense, useState } from 'react'

const personServerFn = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(({ data: name }) => {
return { name, randomNumber: Math.floor(Math.random() * 100) }
})

const slowServerFn = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data: name }) => {
await new Promise((r) => setTimeout(r, 1000))
return { name, randomNumber: Math.floor(Math.random() * 100) }
Expand Down
18 changes: 16 additions & 2 deletions examples/react/start-basic/app/routes/posts.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { Link, Outlet, createFileRoute } from '@tanstack/react-router'
import { fetchPosts } from '../utils/posts'
import { createServerFnClient } from '@tanstack/start'

const serverFnClient = createServerFnClient({
baseUrl:
process.env.NODE_ENV === 'production'
? 'https://my-site.com/_server'
: 'http://localhost:3000/_server',
})

export const Route = createFileRoute('/posts')({
loader: async () => fetchPosts(),
// loader: async () => fetchPosts(),
loader: () =>
serverFnClient
.fetch({
functionId: 'fetchPosts',
method: 'GET',
})
.then((d) => d.result),
component: PostsComponent,
})

Expand Down
8 changes: 4 additions & 4 deletions examples/react/start-basic/app/utils/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export type PostType = {
body: string
}

export const fetchPost = createServerFn({ method: 'GET' })
export const fetchPost = createServerFn({ id: 'fetchPost', method: 'GET' })
.middleware([logMiddleware])
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data }) => {
console.info(`Fetching post with id ${data}...`)
const post = await axios
Expand All @@ -28,8 +28,8 @@ export const fetchPost = createServerFn({ method: 'GET' })
return post
})

export const fetchPosts = createServerFn({ method: 'GET' })
.middleware([logMiddleware])
export const fetchPosts = createServerFn({ id: 'fetchPosts', method: 'GET' })
// .middleware([logMiddleware])
.handler(async () => {
console.info('Fetching posts...')
return axios
Expand Down
2 changes: 1 addition & 1 deletion examples/react/start-supabase-basic/app/utils/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type PostType = {
}

export const fetchPost = createServerFn({ method: 'GET' })
.validator((d) => d as string)
.validator((d: string) => d)
.handler(async ({ data: postId }) => {
console.info(`Fetching post with id ${postId}...`)
const post = await axios
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@
"@tanstack/arktype-adapter": "workspace:*",
"@tanstack/start": "workspace:*",
"@tanstack/start-vite-plugin": "workspace:*",
"@tanstack/eslint-plugin-router": "workspace:*",
"temp-react": "0.0.0-experimental-035a41c4e-20230704",
"temp-react-dom": "0.0.0-experimental-035a41c4e-20230704"
"@tanstack/eslint-plugin-router": "workspace:*"
}
}
}
2 changes: 2 additions & 0 deletions packages/router-plugin/src/core/code-splitter/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export function compileCodeSplitReferenceRoute(opts: ParseAstOptions) {
return generate(ast, {
sourceMaps: true,
sourceFileName: opts.filename,
minified: process.env.NODE_ENV === 'production',
})
}

Expand Down Expand Up @@ -543,6 +544,7 @@ export function compileCodeSplitVirtualRoute(opts: ParseAstOptions) {
return generate(ast, {
sourceMaps: true,
sourceFileName: opts.filename,
minified: process.env.NODE_ENV === 'production',
})
}

Expand Down
5 changes: 5 additions & 0 deletions packages/server-functions-vite-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<img src="https://static.scarf.sh/a.png?x-pxid=d988eb79-b0fc-4a2b-8514-6a1ab932d188" />

# TanStack Start Vite Plugin

See https://tanstack.com/router/latest/docs/framework/react/guide/file-based-routing
5 changes: 5 additions & 0 deletions packages/server-functions-vite-plugin/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @ts-check

import rootConfig from '../../eslint.config.js'

export default [...rootConfig]
89 changes: 89 additions & 0 deletions packages/server-functions-vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"name": "@tanstack/directive-functions-plugin",
"version": "1.87.3",
"description": "Modern and scalable routing for React applications",
"author": "Tanner Linsley",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/TanStack/router.git",
"directory": "packages/directive-functions-plugin"
},
"homepage": "https://tanstack.com/start",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"keywords": [
"react",
"location",
"router",
"routing",
"async",
"async router",
"typescript"
],
"scripts": {
"clean": "rimraf ./dist && rimraf ./coverage",
"test": "pnpm test:eslint && pnpm test:types && pnpm test:build && pnpm test:unit",
"test:unit": "vitest",
"test:unit:dev": "vitest --watch",
"test:eslint": "eslint ./src",
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
"test:types:ts52": "node ../../node_modules/typescript52/lib/tsc.js",
"test:types:ts53": "node ../../node_modules/typescript53/lib/tsc.js",
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js",
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js",
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js",
"test:types:ts57": "tsc",
"test:build": "publint --strict && attw --ignore-rules no-resolution --pack .",
"build": "vite build"
},
"type": "module",
"types": "dist/esm/index.d.ts",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
},
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"dist",
"src"
],
"engines": {
"node": ">=12"
},
"dependencies": {
"@babel/code-frame": "7.26.2",
"@babel/core": "^7.26.0",
"@babel/generator": "^7.26.3",
"@babel/parser": "^7.26.3",
"@babel/plugin-syntax-jsx": "^7.25.9",
"@babel/plugin-syntax-typescript": "^7.25.9",
"@babel/template": "^7.25.9",
"@babel/traverse": "^7.26.4",
"@babel/types": "^7.26.3",
"@types/babel__code-frame": "^7.0.6",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__template": "^7.4.4",
"@types/babel__traverse": "^7.20.6",
"@types/diff": "^6.0.0",
"babel-dead-code-elimination": "^1.0.6",
"chalk": "^5.3.0",
"dedent": "^1.5.3",
"diff": "^7.0.0",
"tiny-invariant": "^1.3.3"
}
}
20 changes: 20 additions & 0 deletions packages/server-functions-vite-plugin/src/ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { parse } from '@babel/parser'
import type { ParseResult } from '@babel/parser'

export type ParseAstOptions = {
code: string
filename: string
root: string
}

export function parseAst(opts: ParseAstOptions): ParseResult<babel.types.File> {
return parse(opts.code, {
plugins: ['jsx', 'typescript'],
sourceType: 'module',
...{
root: opts.root,
filename: opts.filename,
sourceMaps: true,
},
})
}
Loading
Loading