{"id":9866,"date":"2024-04-02T14:41:16","date_gmt":"2024-04-02T12:41:16","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9866"},"modified":"2024-04-08T11:30:00","modified_gmt":"2024-04-08T09:30:00","slug":"astro-tutorial-working-with-preact","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2024\/04\/02\/astro-tutorial-working-with-preact\/","title":{"rendered":"Astro Tutorial &#8211; Working with Preact"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Code for this tutorial is on <a href=\"https:\/\/github.com\/r14r\/Astro_Tutorial_Working-with-Preact\" data-type=\"link\" data-id=\"https:\/\/github.com\/r14r\/Astro_Tutorial_Add-TOC-to-PostLayout\">Github<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Starter Project<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Create Starter<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"10,13,16,19,\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u276f npm create astro@latest \"Tutorial - Working with Preact\"\n...\/21.6.2\/pnpm\/store\/v3\/tmp\/dlx-99714   |  +39 ++++\n...\/21.6.2\/pnpm\/store\/v3\/tmp\/dlx-99714   | Progress: resolved 39, reused 39, downloaded 0, added 39, done\n\n astro   Launch sequence initiated.\n\n      \u25fc  dir Using Tutorial - Working with Preact as project directory\n\n  tmpl   How would you like to start your new project?\n         Empty\n\n    ts   Do you plan to write TypeScript?\n         Yes\n\n   use   How strict should TypeScript be?\n         Strict\n\n  deps   Install dependencies?\n         Yes\n\n   git   Initialize a new git repository?\n         Yes\n\n      \u2714  Project initialized!\n         \u25a0 Template copied\n         \u25a0 TypeScript customized\n         \u25a0 Dependencies installed\n         \u25a0 Git initialized\n\n  next   Liftoff confirmed. Explore your project!\n\n         Enter your project directory using cd \".\/Tutorial - Working with Preact\"\n         Run pnpm dev to start the dev server. CTRL+C to stop.\n         Add frameworks like react or tailwind using astro add.\n\n         Stuck? Join us at https:\/\/astro.build\/chat\n\n\u256d\u2500\u2500\u2500\u2500\u2500\u256e  Houston:\n\u2502 \u25e0 \u25e1 \u25e0  Good luck out there, astronaut! \ud83d\ude80\n\u2570\u2500\u2500\u2500\u2500\u2500\u256f<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Run Starter<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u276f cd \"Tutorial - Working with Preact\"\n\u276f npm run dev\n\n> tutorial---working-with-preact@0.0.1 dev \/Users\/Shared\/CLOUD\/Programmier-Workshops\/Kurse\/Astro\/Einsteiger\/Tutorial - Working with Preact\n> astro dev\n\n\n astro  v4.5.13 ready in 156 ms\n\n\u2503 Local    http:\/\/localhost:4321\/\n\u2503 Network  use --host to expose\n\n14:34:39 watching for file changes...<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"373\" src=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-1024x373.png\" alt=\"\" class=\"wp-image-9868\" srcset=\"https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-1024x373.png 1024w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-300x109.png 300w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-768x279.png 768w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-1536x559.png 1536w, https:\/\/via-internet.de\/blog\/wp-content\/uploads\/2024\/04\/Astro-Template-Blank-2048x745.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Modify Project Structur and add helper code<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">tsconfig.json<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"6,9\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"extends\": \"astro\/tsconfigs\/strict\",\n  \"compilerOptions\": {\n    \"allowJs\": true,\n    \"strictNullChecks\": true,\n    \"baseUrl\": \".\",\n    \"lib\": [\"es2022\", \"dom\", \"dom.iterable\"],\n    \"paths\": {\n      \"@\/*\": [\"src\/*\"]\n    }\n  },\n  \"exclude\": [\"node_modules\", \"**\/node_modules\/*\", \".vscode\", \"dist\"]\n}\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Replace relative path with reference to path alias in every page<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Layout from '..\/..\/layouts\/BlogPost.astro';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">becomes to <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Layout from '@\/layouts\/BlogPost.astro';<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Layout from '..\/styles\/global.css';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">becomes to <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Layout from '@\/styles\/global.css';<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Install Preact Integration<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Add integration for preact<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u276f npx astro add preact\n\u2714 Resolving packages...\n\n  Astro will run the following command:\n  If you skip this step, you can always run it yourself later\n\n \u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n \u2502 pnpm add @astrojs\/preact@^3.1.2 preact@^10.20.1  \u2502\n \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\n\n\u2714 Continue? \u2026 yes\n\u2714 Installing dependencies...\n\n  Astro will make the following changes to your config file:\n\n \u256d astro.config.mjs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n \u2502 import { defineConfig } from 'astro\/config';  \u2502\n \u2502                                               \u2502\n \u2502 import preact from \"@astrojs\/preact\";         \u2502\n \u2502                                               \u2502\n \u2502 \/\/ https:\/\/astro.build\/config                 \u2502\n \u2502 export default defineConfig({                 \u2502\n \u2502   integrations: [preact()]                    \u2502\n \u2502 });                                           \u2502\n \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\n\n\u2714 Continue? \u2026 yes\n\n   success  Added the following integration to your project:\n  - @astrojs\/preact\n\n  Astro will make the following changes to your tsconfig.json:\n\n \u256d tsconfig.json \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n \u2502 {                                                                    \u2502\n \u2502   \"extends\": \"astro\/tsconfigs\/strict\",                               \u2502\n \u2502   \"compilerOptions\": {                                               \u2502\n \u2502     \"allowJs\": true,                                                 \u2502\n \u2502     \"strictNullChecks\": true,                                        \u2502\n \u2502     \"baseUrl\": \".\",                                                  \u2502\n \u2502     \"lib\": [                                                         \u2502\n \u2502       \"es2022\",                                                      \u2502\n \u2502       \"dom\",                                                         \u2502\n \u2502       \"dom.iterable\"                                                 \u2502\n \u2502     ],                                                               \u2502\n \u2502     \"paths\": {                                                       \u2502\n \u2502       \"@\/*\": [                                                       \u2502\n \u2502         \"src\/*\"                                                      \u2502\n \u2502       ]                                                              \u2502\n \u2502     },                                                               \u2502\n \u2502     \"jsx\": \"react-jsx\",                                              \u2502\n \u2502     \"jsxImportSource\": \"preact\"                                      \u2502\n \u2502   },                                                                 \u2502\n \u2502   \"exclude\": [                                                       \u2502\n \u2502     \"node_modules\",                                                  \u2502\n \u2502     \"**\/node_modules\/*\",                                             \u2502\n \u2502     \".vscode\",                                                       \u2502\n \u2502     \"dist\"                                                           \u2502\n \u2502   ]                                                                  \u2502\n \u2502 }                                                                    \u2502\n \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\n\n\u2714 Continue? \u2026 yes\n\n   success  Successfully updated TypeScript settings<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install preact\/signale<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm install preact\/signal<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Modify src\/pages\/index.astro<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">---\nimport Counter from \"@\/components\/Counter\";\nimport { signal } from \"@preact\/signals\";\n\nconst count = signal(0);\n---\n\n&lt;html lang=\"en\">\n  &lt;head>\n    &lt;meta charset=\"utf-8\" \/>\n    &lt;meta name=\"viewport\" content=\"width=device-width\" \/>\n    &lt;meta name=\"generator\" content={Astro.generator} \/>\n    &lt;link rel=\"icon\" type=\"image\/svg+xml\" href=\"\/favicon.svg\" \/>\n    &lt;style>\n      html,\n      body {\n        font-family: system-ui;\n        margin: 0;\n      }\n      body { padding: 2rem; }\n      h1 { text-align: center; }\n    &lt;\/style>\n  &lt;\/head>\n  &lt;body>\n    &lt;main>\n      &lt;h1 style=\"halign:center\">Working with Astro and Preact&lt;\/h1>\n      &lt;Counter count={count} client:visible>&lt;h3>Hello, Preact 1!&lt;\/h3>&lt;\/Counter>\n      &lt;Counter count={count} client:visible>&lt;h3>Hello, Preact 2!&lt;\/h3>&lt;\/Counter>\n    &lt;\/main>\n  &lt;\/body>\n&lt;\/html>\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create Component Counter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create folder <code>src\/components<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir src\/components<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create file Counter.tsx<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import type { ComponentChildren } from \"preact\";\nimport type { Signal } from \"@preact\/signals\";\nimport { lazy, Suspense } from \"preact\/compat\";\n\nimport \".\/Counter.css\";\n\nconst Message = lazy(async () => import(\".\/Message\"));\nconst Fallback = () => &lt;p>Loading...&lt;\/p>;\n\ntype Props = {\n  children: ComponentChildren;\n  count: Signal&lt;number>;\n};\n\nexport default function Counter({ children, count }: Props) {\n  const add = () => count.value++;\n  const subtract = () => count.value--;\n\n  return (\n    &lt;>\n      &lt;div class=\"counter\">\n        &lt;button onClick={subtract}>-&lt;\/button>\n        &lt;pre>{count}&lt;\/pre>\n        &lt;button onClick={add}>+&lt;\/button>\n      &lt;\/div>\n      &lt;Suspense fallback={Fallback}>\n        &lt;Message>{children}&lt;\/Message>\n      &lt;\/Suspense>\n    &lt;\/>\n  );\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create file Counter.css<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.counter {\n\tdisplay: grid;\n\tfont-size: 2em;\n\tgrid-template-columns: repeat(3, minmax(0, 1fr));\n\tmargin-top: 2em;\n\tplace-items: center;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create Message.tsx<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import type { ComponentChildren } from 'preact';\nimport '.\/Message.css';\n\nexport default function Message({ children }: { children: ComponentChildren }) {\n\treturn &lt;div class=\"message\">{children}&lt;\/div>;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create Message.css<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.message {\n\ttext-align: center;\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Run App<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm run dev<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR Code for this tutorial is on Github Starter Project Create Starter Run Starter Modify Project Structur and add helper code tsconfig.json Replace relative path with reference to path alias in every page becomes to becomes to Install Preact Integration Add integration for preact Install preact\/signale Modify src\/pages\/index.astro Create Component Counter Create folder src\/components Create file Counter.tsx Create file Counter.css Create Message.tsx Create Message.css Run App<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[160],"tags":[],"class_list":["post-9866","post","type-post","status-publish","format-standard","hentry","category-astro"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9866"}],"version-history":[{"count":7,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9866\/revisions"}],"predecessor-version":[{"id":9879,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9866\/revisions\/9879"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}