-
-
Couldn't load subscription status.
- Fork 6
feat: add qwik example #9
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
Draft
gioboa
wants to merge
3
commits into
nitrojs:main
Choose a base branch
from
gioboa:feat/add-qwik
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,562
−162
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -26,3 +26,7 @@ package-lock.json | |
| *.iml | ||
| *.lcov | ||
| *.log | ||
|
|
||
| # qwik | ||
| /examples/qwik/tmp | ||
| /examples/qwik/server | ||
This file contains hidden or 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,23 @@ | ||
| { | ||
| "name": "nitro-vite-qwik", | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "qwik build", | ||
| "build.client": "vite build", | ||
| "build.preview": "vite build --ssr", | ||
| "build.types": "tsc --incremental --noEmit", | ||
| "dev": "vite --open --mode ssr", | ||
| "preview": "qwik build preview && cp -r ./dist/* ./.output/public/ && vite preview --open", | ||
| "qwik": "qwik" | ||
| }, | ||
| "devDependencies": { | ||
| "@qwik.dev/core": "^2.0.0-beta.11", | ||
| "@qwik.dev/router": "^2.0.0-beta.11", | ||
| "nitro": "npm:nitro-nightly", | ||
| "node-fetch-native": "^1.6.7", | ||
| "typescript": "5.9.3", | ||
| "vite": "7.1.10", | ||
| "vite-tsconfig-paths": "^5.1.4" | ||
| } | ||
| } | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,9 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/web-manifest-combined.json", | ||
| "name": "qwik-project-name", | ||
| "short_name": "Welcome to Qwik", | ||
| "start_url": ".", | ||
| "display": "standalone", | ||
| "background_color": "#fff", | ||
| "description": "A Qwik project app." | ||
| } |
This file contains hidden or 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,4 @@ | ||
| import { createQwikRouter } from "@qwik.dev/router/middleware/node"; | ||
| import render from "./entry.ssr"; | ||
|
|
||
| export default createQwikRouter({ render }); |
This file contains hidden or 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,17 @@ | ||
| import { createQwikRouter } from "./qwik-fetch-adapter"; | ||
| import render from "./entry.ssr"; | ||
|
|
||
| const { router, notFound } = createQwikRouter({ render }); | ||
|
|
||
| export default { | ||
| fetch: async (req: Request) => { | ||
| console.log(`[${req.method}] ${req.url}`); | ||
| const qwikRouterResponse = await router(req); | ||
| if (qwikRouterResponse) { | ||
| return qwikRouterResponse; | ||
| } | ||
|
|
||
| // Path not found | ||
| return notFound(req); | ||
| }, | ||
| }; |
This file contains hidden or 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,16 @@ | ||
| import { createRenderer } from "@qwik.dev/router"; | ||
| import Root from "./root"; | ||
|
|
||
| export default createRenderer((opts) => { | ||
| return { | ||
| jsx: <Root />, | ||
| options: { | ||
| ...opts, | ||
| containerAttributes: { | ||
| lang: "en-us", | ||
| ...opts.containerAttributes, | ||
| }, | ||
| serverData: { ...opts.serverData }, | ||
| }, | ||
| }; | ||
| }); |
This file contains hidden or 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,3 @@ | ||
| .pt-2 { | ||
| padding-top: 2rem; | ||
| } |
This file contains hidden or 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,126 @@ | ||
| import { setServerPlatform } from "@qwik.dev/core/server"; | ||
| import type { | ||
| ClientConn, | ||
| ServerRenderOptions, | ||
| ServerRequestEvent, | ||
| } from "@qwik.dev/router/middleware/request-handler"; | ||
| import { | ||
| getNotFound, | ||
| isStaticPath, | ||
| mergeHeadersCookies, | ||
| requestHandler, | ||
| } from "@qwik.dev/router/middleware/request-handler"; | ||
|
|
||
| /** @public */ | ||
| export interface NetAddr { | ||
| transport: "tcp" | "udp"; | ||
| hostname: string; | ||
| port: number; | ||
| } | ||
|
|
||
| /** @public */ | ||
| export function createQwikRouter(opts: QwikRouterFetchOptions) { | ||
| if (opts.manifest) { | ||
| setServerPlatform(opts.manifest); | ||
| } | ||
| async function router(request: Request) { | ||
| try { | ||
| const url = new URL(request.url); | ||
|
|
||
| const serverRequestEv: ServerRequestEvent<Response> = { | ||
| mode: "server", | ||
| locale: undefined, | ||
| url, | ||
| env: { get: (p: string) => process.env[p] }, | ||
| request, | ||
| getWritableStream: (status, headers, cookies, resolve) => { | ||
| const { readable, writable } = new TransformStream<Uint8Array>(); | ||
| const response = new Response(readable, { | ||
| status, | ||
| headers: mergeHeadersCookies(headers, cookies), | ||
| }); | ||
| resolve(response); | ||
| return writable; | ||
| }, | ||
| platform: { | ||
| ssr: true, | ||
| }, | ||
| getClientConn: () => { | ||
| return opts.getClientConn ? opts.getClientConn(request) : {}; | ||
| }, | ||
| }; | ||
|
|
||
| // send request to qwik router request handler | ||
| const handledResponse = await requestHandler(serverRequestEv, opts); | ||
| if (handledResponse) { | ||
| handledResponse.completion.then((v) => { | ||
| if (v) { | ||
| console.error(v); | ||
| } | ||
| }); | ||
| const response = await handledResponse.response; | ||
| if (response) { | ||
| return response; | ||
| } | ||
| } | ||
|
|
||
| // qwik router did not have a route for this request | ||
| return null; | ||
| } catch (e: any) { | ||
| console.error(e); | ||
| return new Response(String(e || "Error"), { | ||
| status: 500, | ||
| headers: { | ||
| "Content-Type": "text/plain; charset=utf-8", | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const notFound = async (request: Request) => { | ||
| try { | ||
| const url = new URL(request.url); | ||
|
|
||
| // In the development server, we replace the getNotFound function | ||
| // For static paths, we assign a static "Not Found" message. | ||
| // This ensures consistency between development and production environments for specific URLs. | ||
| const notFoundHtml = | ||
| !request.headers.get("accept")?.includes("text/html") || | ||
| isStaticPath(request.method || "GET", url) | ||
| ? "Not Found" | ||
| : getNotFound(url.pathname); | ||
| return new Response(notFoundHtml, { | ||
| status: 404, | ||
| headers: { | ||
| "Content-Type": "text/html; charset=utf-8", | ||
| "X-Not-Found": url.pathname, | ||
| }, | ||
| }); | ||
| } catch (e) { | ||
| console.error(e); | ||
| return new Response(String(e || "Error"), { | ||
| status: 500, | ||
| headers: { | ||
| "Content-Type": "text/plain; charset=utf-8", | ||
| }, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| router, | ||
| notFound, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated Use `createQwikRouter` instead. Will be removed in V3 | ||
| * @public | ||
| */ | ||
| export const createQwikCity = createQwikRouter; | ||
|
|
||
| /** @public */ | ||
| export interface QwikRouterFetchOptions | ||
| extends Omit<ServerRenderOptions, "qwikCityPlan"> { | ||
| getClientConn?: (request: Request) => ClientConn; | ||
| } |
This file contains hidden or 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,31 @@ | ||
| import { | ||
| DocumentHeadTags, | ||
| RouterOutlet, | ||
| useLocation, | ||
| useQwikRouter, | ||
| } from "@qwik.dev/router"; | ||
| import { component$ } from "@qwik.dev/core"; | ||
|
|
||
| import "./global.css"; | ||
|
|
||
| export default component$(() => { | ||
| useQwikRouter(); | ||
| const { url } = useLocation(); | ||
|
|
||
| return ( | ||
| <> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> | ||
|
|
||
| <DocumentHeadTags /> | ||
|
|
||
| <link rel="canonical" href={url.href} /> | ||
| </head> | ||
| <body> | ||
| <RouterOutlet /> | ||
| </body> | ||
| </> | ||
| ); | ||
| }); |
This file contains hidden or 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,64 @@ | ||
| import { | ||
| Form, | ||
| routeAction$, | ||
| routeLoader$, | ||
| server$, | ||
| type DocumentHead, | ||
| } from "@qwik.dev/router"; | ||
| import { component$, useSignal } from "@qwik.dev/core"; | ||
|
|
||
| export const useLoader = routeLoader$(() => { | ||
| return "data from routeLoader$"; | ||
| }); | ||
|
|
||
| export const useAction = routeAction$(async (data) => { | ||
| return { | ||
| success: true, | ||
| firstName: data.firstName.toString(), | ||
| lastName: data.lastName.toString(), | ||
| }; | ||
| }); | ||
|
|
||
| const serverFunction = server$(() => { | ||
| console.log("server function"); | ||
| return "data from server$, look at the server console!"; | ||
| }); | ||
|
|
||
| export default component$(() => { | ||
| const action = useAction(); | ||
| const loaderData = useLoader(); | ||
| const counterSig = useSignal(0); | ||
|
|
||
| return ( | ||
| <div> | ||
| <h1>Hello, Qwik!</h1> | ||
| <h2>{loaderData.value}</h2> | ||
| <button onClick$={() => (counterSig.value += 1)}> | ||
| Count: {counterSig.value} | ||
| </button> | ||
| <br></br> | ||
| <br></br> | ||
| <button | ||
| onClick$={async () => { | ||
| const res = await serverFunction(); | ||
| alert(res); | ||
| }} | ||
| > | ||
| server$ function | ||
| </button> | ||
| <Form class="pt-2" action={action}> | ||
| <input name="firstName" /> | ||
| <input name="lastName" /> | ||
| <button type="submit">Add user</button> | ||
| </Form> | ||
| {action.value?.success && ( | ||
| <p class="pt-2"> | ||
| User {action.value.firstName} {action.value.lastName} added | ||
| successfully | ||
| </p> | ||
| )} | ||
| </div> | ||
| ); | ||
| }); | ||
|
|
||
| export const head: DocumentHead = { title: "Vite + Nitro + Qwik", meta: [] }; |
This file contains hidden or 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,13 @@ | ||
| import { component$, Slot } from "@qwik.dev/core"; | ||
|
|
||
| export default component$(() => { | ||
| return ( | ||
| <> | ||
| <header>Layout Header</header> | ||
| <main> | ||
| <Slot /> | ||
| </main> | ||
| <footer>Layout Footer</footer> | ||
| </> | ||
| ); | ||
| }); |
This file contains hidden or 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,25 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowJs": true, | ||
| "target": "ES2020", | ||
| "module": "ES2022", | ||
| "lib": ["es2022", "DOM", "WebWorker", "DOM.Iterable"], | ||
| "jsx": "react-jsx", | ||
| "jsxImportSource": "@qwik.dev/core", | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "resolveJsonModule": true, | ||
| "allowImportingTsExtensions": true, | ||
| "moduleResolution": "Bundler", | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "incremental": true, | ||
| "isolatedModules": true, | ||
| "outDir": "tmp", | ||
| "noEmit": true, | ||
| "paths": { | ||
| "~/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src", "./*.d.ts", "./*.config.ts"] | ||
| } |
This file contains hidden or 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,17 @@ | ||
| import { qwikVite } from "@qwik.dev/core/optimizer"; | ||
| import { qwikRouter } from "@qwik.dev/router/vite"; | ||
| import { defineConfig, type UserConfig } from "vite"; | ||
| import tsconfigPaths from "vite-tsconfig-paths"; | ||
| import { nitro } from "nitro/vite"; | ||
|
|
||
| export default defineConfig(() => { | ||
| return { | ||
| plugins: [ | ||
| qwikRouter({ devSsrServer: false }), | ||
| qwikVite({ ssr: { input: "src/entry.server" } }), | ||
| tsconfigPaths({ root: "." }), | ||
| nitro(), | ||
| ], | ||
| build: { rollupOptions: { external: [/^node/] } }, | ||
| }; | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cp step is needed b/c some files are missing in the .output folder. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this extra step to have the possibility to use nitro presets 👀