chore: update nano-jsx example (#97)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 1b65c44 · Browse files · 15 files·+174 added−150 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
15 files+174 added−150 removed
- other:+174 added−150 removed
Provenance
- Commit
24f4a7c- Tree
992e9c3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1b65c44
15 files changed+174 added−150 removed
added:examples/jsx-ssr/src/components/footer.tsx+11 added
1234567891011import Nano from 'nano-jsx'
export const Footer = () => { return ( <footer> <address> Built with <a href='https://github.com/yusukebe/hono'>Hono</a> </address> </footer> )}added:examples/jsx-ssr/src/components/header.tsx+11 added
1234567891011import Nano from 'nano-jsx'
export const Header = () => { return ( <header> <h1> <a href='/'>Hono Example</a> </h1> </header> )}added:examples/jsx-ssr/src/pages/page.tsx+25 added
12345678910111213141516171819202122232425import Nano from 'nano-jsx'import { Helmet } from 'nano-jsx'import { Header } from '../components/header'import { Footer } from '../components/footer'import type { Post } from '../index'
type Props = { post: Post}
export const Page = (props: Props) => { return ( <div> <Helmet> <title>{props.post.title}</title> </Helmet> <Header /> <main> <h2>{props.post.title}</h2> <p>{props.post.body}</p> </main> <Footer /> </div> )}added:examples/jsx-ssr/src/pages/top.tsx+34 added
12345678910111213141516171819202122232425262728293031323334import Nano from 'nano-jsx'import { Helmet } from 'nano-jsx'import { Header } from '../components/header'import { Footer } from '../components/footer'import type { Post } from '../index'
type Props = { posts: Post[]}
export const Top = (props: Props) => { return ( <div> <Helmet> <title>Top</title> </Helmet> <Header /> <main> <h2>Posts</h2> {props.posts} <ul> {props.posts.map((post) => { return ( <li> <a href={`/post/${post.id}`}>{post.title}</a> </li> ) })} </ul> </main> <Footer /> </div> )}deleted:examples/jsx-ssr/src/Footer.jsx-7 removed
1234567export const Footer = () => { return ( <footer> <address><a href="https://github.com/yusukebe/hono">Hono</a></address> </footer> )}No newline at end of filedeleted:examples/jsx-ssr/src/Header.jsx-7 removed
1234567export const Header = () => { return ( <header> <h1><a href="/">Hono Example</a></h1> </header> )}deleted:examples/jsx-ssr/src/index.js-61 removed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import { Hono } from '../../../dist/index'import Nano from 'nano-jsx'import { Post } from './Post'import { Top } from './Top'
const app = new Hono()
// Viewconst makeHTML = (body) => { return `<!DOCTYPE html> <html> <body> ${body} </body></html>`}
// Modelconst posts = [ { id: '1', title: 'Good Morning', body: 'Let us eat breakfast' }, { id: '2', title: 'Good Afternoon', body: 'Let us eat Lunch' }, { id: '3', title: 'Good Evening', body: 'Let us eat Dinner' }, { id: '4', title: 'Good Night', body: 'Let us drink Beer' }, { id: '5', title: 'こんにちは', body: '昼からビールを飲みます' },]
// Logicconst getPosts = () => { return posts}
const getPost = (id) => { for (const post of posts) { if (post.id === id) { return post } }}
// Controllerapp.get('/', (c) => { const posts = getPosts() const body = Nano.renderSSR(<Top posts={posts} />) const html = makeHTML(body) return c.html(html)})
app.get('/post/:id{[0-9]+}', (c) => { const id = c.req.param('id') const post = getPost(id) if (!post) { return c.text('Not Found', 404) } const body = Nano.renderSSR(<Post post={post} />) const html = makeHTML(body) return c.html(html)})
app.fire()added:examples/jsx-ssr/src/index.tsx+50 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import { Hono } from 'hono'import Nano from 'nano-jsx'import { render } from './renderer'import { Page } from './pages/page'import { Top } from './pages/top'
const app = new Hono()
// Modelexport type Post = { id: string title: string body: string}
const posts: Post[] = [ { id: '1', title: 'Good Morning', body: 'Let us eat breakfast' }, { id: '2', title: 'Good Afternoon', body: 'Let us eat Lunch' }, { id: '3', title: 'Good Evening', body: 'Let us eat Dinner' }, { id: '4', title: 'Good Night', body: 'Let us drink Beer' }, { id: '5', title: 'こんにちは', body: '昼からビールを飲みます' },]
// Logicconst getPosts = () => { return posts}
const getPost = (id: string) => { return posts.find((post) => post.id == id)}
// Controllerapp.get('/', (c) => { const posts = getPosts() const html = render(<Top posts={posts} />) return c.html(html)})
app.get('/post/:id{[0-9]+}', (c) => { const id = c.req.param('id') const post = getPost(id) if (!post) { return c.text('Not Found', 404) } const html = render(<Page post={post} />) return c.html(html)})
app.fire()deleted:examples/jsx-ssr/src/Post.jsx-15 removed
1-import { Header } from './Header'2-import { Footer } from './Footer'3-4-export const Post = (props) => {5- return (6- <div>7- <Header />8- <main>9- <h2>{props.post.title}</h2>10- <p>{props.post.body}</p>11- </main>12- <Footer />13- </div>14- )15-}added:examples/jsx-ssr/src/renderer.tsx+22 added
1+import Nano from 'nano-jsx'2+import { Helmet } from 'nano-jsx'3+4+export const render = (component: any) => {5+ const app = Nano.renderSSR(component)6+ const { body, head, footer, attributes } = Helmet.SSR(app)7+ const html = `8+<!DOCTYPE html>9+<html ${attributes.html.toString()}>10+ <head>11+ <meta charset="UTF-8">12+ <meta name="viewport" content="width=device-width, initial-scale=1.0">13+ <link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/mini.css/3.0.1/mini-default.min.css' />14+ ${head.join('\n')}15+ </head>16+ <body ${attributes.body.toString()} style="padding: 1em 2em">17+ ${body}18+ ${footer.join('\n')}19+ </body>20+</html>`21+ return html22+}deleted:examples/jsx-ssr/src/Top.jsx-20 removed
1-import { Header } from './Header'2-import { Footer } from './Footer'3-4-export const Top = (props) => {5- return (6- <div>7- <Header />8- <main>9- <h2>Posts</h2>10- {props.posts}11- <ul>12- {props.posts.map((post) => {13- return <li><a href={`/post/${post.id}`}>{post.title}</a></li>14- })}15- </ul>16- </main>17- <Footer />18- </div>19- )20-}modified:examples/jsx-ssr/package.json+10 added-12 removed
1 {2 "name": "hono-example-jsx-ssr",3 "version": "0.0.1",4 "description": "",5- "main": "index.js",5+ "main": "dist/index.js",6 "scripts": {7- "build": "tsc && wrangler build",8- "dev": "tsc && npm-run-all --parallel dev:*",9- "dev:tsc": "tsc --watch",10- "dev:wrangler": "wrangler dev",11- "test": "echo \"Error: no test specified\" && exit 1"7+ "build": "esbuild --bundle --sourcemap --outdir=dist ./src/index.tsx",8+ "dev": "miniflare --live-reload --debug",9+ "publish": "wrangler publish dist/index.js"10 },11 "repository": {12 "type": "git",13 "url": "git+https://github.com/yusukebe/hono.git"4 unmodified lines18 "url": "https://github.com/yusukebe/hono/issues"19 },20 "homepage": "https://github.com/yusukebe/hono#readme",21 "dependencies": {24- "hono": "*",25- "nano-jsx": "^0.0.27"22+ "hono": "^0.3.7",23+ "nano-jsx": "^0.0.29"24 },25 "devDependencies": {28- "@cloudflare/wrangler": "^1.19.7",29- "npm-run-all": "^4.1.5",30- "typescript": "^4.5.4"26+ "esbuild": "^0.14.20",27+ "miniflare": "^2.2.0",28+ "wrangler": "^0.0.16"29 }32-}30+}modified:examples/jsx-ssr/tsconfig.json+3 added-17 removed
1 {2 "compilerOptions": {3- "jsx": "react-jsx",4- "jsxImportSource": "nano-jsx/lib",5- "target": "es2017",6- "module": "commonjs",7- "rootDir": "src",8- "outDir": "./dist",9- "allowJs": true,10- "strict": true,11- "esModuleInterop": true,12- "skipLibCheck": true,13- },14- "include": [15- "src/**/*"16- ],17- "exclude": [18- "node_modules",19- ]3+ "jsx": "react",4+ "jsxFactory": "Nano.h",5+ }6 }deleted:examples/jsx-ssr/webpack.config.js-4 removed
1-module.exports = {2- target: 'webworker',3- entry: './dist/index.js',4-}modified:examples/jsx-ssr/wrangler.toml+8 added-7 removed
1 name = "hono-example-jsx-ssr"2-type = "webpack"3-route = ''4-zone_id = ''5-usage_model = ''6-compatibility_flags = []7-workers_dev = true2+type = "javascript"3+4 compatibility_date = "2022-01-08"9-webpack_config = "webpack.config.js"5+6+[build]7+command = "yarn run build"8+9+[build.upload]10+format = "service-worker"