|
| 1 | +# Astro Starlight On GitHub Pages |
| 2 | + |
| 3 | +> Objectives |
| 4 | +
|
| 5 | +Build and deploy an Astro Starlight Documentation site to GitHub Pages. |
| 6 | +Explore features and customization - validate its use as reusable template. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 1. Why Astro? |
| 11 | + |
| 12 | +Astro is a free, open-source option for static site generation that bills itself as _the all-in-one web framework designed for speed_. Three features that make it interesting: |
| 13 | + - Islands architecture - with **zero client-side JS** |
| 14 | + - Plays well with others - **bring your own components** |
| 15 | + - Rich ecosystem - **content focused & community-driven** |
| 16 | + |
| 17 | +This gives you the benefits of performance & flexibility, with rich themes and integrations for quickstart adoption. It's a rising star for JS frameworks and used by industry teams - stability ftw. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 2. Why Starlight? |
| 22 | + |
| 23 | +Astro has a [large collection of themes](https://astro.build/themes) supporting different site categories and frontend technology components. The ["Official" filter](https://astro.build/themes?search=&categories%5B%5D=official) identifies themes created by the Astro team. [Starlight](https://astro.build/themes/details/starlight/) is the default **documentation focused** framework from Astro, currently in very early release. It promises: |
| 24 | + - fast, accessible, easy-to-use websites |
| 25 | + - site-navigation, search, i18n, SEO support |
| 26 | + - code highlighting, dark mode, easy-to-read typography |
| 27 | + - write in Markdown, MDX or Markdoc |
| 28 | + |
| 29 | +Plus all Astro benefits (e.g., bring your own UI components). |
| 30 | + |
| 31 | + - [See the GitHub Repo](https://github.com/withastro/starlight) |
| 32 | + - [Read the Docs](https://starlight.astro.build/) |
| 33 | + - [Get Started](https://starlight.astro.build/getting-started/) |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 3. Quickstart |
| 38 | + |
| 39 | +1. Verify you have Node.js installed. I use `nvm` and default to the LTS version for Node.js. |
| 40 | + |
| 41 | + ```bash |
| 42 | + $ nvm use --lts |
| 43 | + Now using node v18.16.0 (npm v9.6.7) |
| 44 | + ``` |
| 45 | + |
| 46 | +2. Scaffold out a Starlight project with Astro. |
| 47 | + |
| 48 | + ```bash |
| 49 | + $ npm create astro@latest -- --template starlight |
| 50 | + ``` |
| 51 | + |
| 52 | + As part of setup, you define the destination folder (`website`), install dependencies and configure Typescript, git usage if needed. You can also disable telemetry capture using `npx astro telemetry disable` at this time. |
| 53 | + |
| 54 | +3. Preview the default Starlight site. |
| 55 | + |
| 56 | + ```bash |
| 57 | + $ cd website |
| 58 | + $ npm run dev |
| 59 | + ``` |
| 60 | + |
| 61 | + This runs a dev server on `http://localhost:3001` which watches `src/content` for changes (hot reload). |
| 62 | + |
| 63 | +4. Open the browser to that URL and let's see what we got: |
| 64 | +
|
| 65 | + > A Landing Page (Light Mode) |
| 66 | +
|
| 67 | +  |
| 68 | +
|
| 69 | + > A Landing Page (Dark Mode) |
| 70 | +
|
| 71 | +  |
| 72 | +
|
| 73 | + > A Documentation Page (Default) |
| 74 | +
|
| 75 | +  |
| 76 | +
|
| 77 | + > The Documentation Page Updated (Hot Reload) |
| 78 | +
|
| 79 | +  |
| 80 | +
|
| 81 | + > The Search Feature (oh-oh!) |
| 82 | +
|
| 83 | +  |
| 84 | +
|
| 85 | +5. Alright, let's try to build the **production** version of the site locally. |
| 86 | + |
| 87 | + ```bash |
| 88 | + $ cd website |
| 89 | + $ npm run build |
| 90 | + ... |
| 91 | + ... |
| 92 | + Finished in 0.13 seconds |
| 93 | + 08:53:44 PM [build] 4 page(s) built in 4.61s |
| 94 | + 08:53:44 PM [build] Complete! |
| 95 | + ``` |
| 96 | +6. You'll notice this builds the production version in the `dist` folder. Let's preview it. |
| 97 | + |
| 98 | + ```bash |
| 99 | + $ npm run preview |
| 100 | + ``` |
| 101 | + The output indicates the production server is running at `http://localhost:3000` - let's open that up. You see the same pages as before - but now let's try search. _In fact, let's search for the changed text from above to see if it can be found_. |
| 102 | +
|
| 103 | + > Search for "Related References" in Production |
| 104 | +
|
| 105 | + OMG - it works!! We didn't have to do anything extra to activate search indexes. Basic keyword search out of the box! |
| 106 | + |
| 107 | +  |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 4. Deployment |
| 112 | + |
| 113 | +Before we explore deploying the production build to GitHub Pages, let's commit the current version. **Done!** |
| 114 | +
|
| 115 | +Now, let's [deploy the Astro Site to GitHub Pages](https://docs.astro.build/en/guides/deploy/github/). Astro provides an official `withastro/action` that should make this easy. |
| 116 | + |
| 117 | + 1. Set `site` and `base` options in `astro.config.js` |
| 118 | + 2. Create `.github/workflows/deploy.yml` and copy [the provided workflow](https://docs.astro.build/en/guides/deploy/github/). |
| 119 | + 3. Since we have our site source in the `website/` subfolder (vs. root of repo), uncomment the `with` section of the install steps in workflow and set the `path` to `./website` |
| 120 | + 4. Go to the GitHub repo's [Settings > Pages](https://github.com/30DaysOf/astro-starlight-ghpages/settings/pages) configuration. Choose `GitHub Actions` as the Source of your site. |
| 121 | + |
| 122 | +**Commit the changes in your code to GitHub**. You should see the deploy action run. If successful, the GitHub Pages endpoint should show the deployed site. It's LIVE! [https://30daysof.github.io/astro-starlight-ghpages/](https://30daysof.github.io/astro-starlight-ghpages/)!! |
| 123 | + |
| 124 | + |
| 125 | +> Issue: Hero links not resolving base path correctly |
| 126 | + |
| 127 | +The "Example Guide" button on the landing page is mapped to "/guides/example/" but when clicked, does not take base prefix (repo path) into account, resulting in a 404. The same route used from the sidebar works just fine. **I am assuming this has to do with the difference in how links are resolved in frontmatter vs. markdown** _Issue raised in community chat. Waiting for response_. |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## 7. Adding Blog |
| 132 | + |
| 133 | +The [Starlight Blog](https://starlight-blog-docs.vercel.app/getting-started/) plugin from the community adds the blog feature to the default Starlight theme. Let's try it out. |
| 134 | +
|
| 135 | +1. Install the plugin. |
| 136 | +
|
| 137 | + ```bash |
| 138 | + $ npm install starlight-blog --save |
| 139 | + ``` |
| 140 | +2. Add the plugin to `astro.config.mjs` and configure it. |
| 141 | +
|
| 142 | + ```javascript |
| 143 | + import starlight from '@astrojs/starlight' |
| 144 | + import { defineConfig } from 'astro/config' |
| 145 | + import starlightBlog from 'starlight-blog' |
| 146 | +
|
| 147 | + export default defineConfig({ |
| 148 | + integrations: [ |
| 149 | + starlight({ |
| 150 | + plugins: [starlightBlog()], |
| 151 | + title: 'My Docs', |
| 152 | + }), |
| 153 | + ], |
| 154 | + }) |
| 155 | + ``` |
| 156 | +3. Customize [configuration](https://starlight-blog-docs.vercel.app/configuration) if needed. |
| 157 | +
|
| 158 | + ```javascript |
| 159 | + import starlight from '@astrojs/starlight' |
| 160 | + import { defineConfig } from 'astro/config' |
| 161 | + import starlightBlog from 'starlight-blog' |
| 162 | +
|
| 163 | + export default defineConfig({ |
| 164 | + integrations: [ |
| 165 | + starlight({ |
| 166 | + plugins: [ |
| 167 | + starlightBlog({ |
| 168 | + title: "Blog", |
| 169 | + postCount: 7, |
| 170 | + recentPostCount: 1, |
| 171 | + authors: { |
| 172 | + nitya: { |
| 173 | + name: "Nitya", |
| 174 | + picture: "https://github.com/nitya.png", |
| 175 | + url: "https://github.com/nitya", |
| 176 | + title: "AI, Art & Advocacy @Microsoft", |
| 177 | + } |
| 178 | + }, |
| 179 | + }), |
| 180 | + ], |
| 181 | + title: 'My Docs', |
| 182 | + }), |
| 183 | + ], |
| 184 | + }) |
| 185 | + ``` |
| 186 | + 4. Extend the frontmatter schema |
| 187 | +
|
| 188 | + ```javascript |
| 189 | + import { defineCollection } from 'astro:content'; |
| 190 | + import { docsSchema, i18nSchema } from '@astrojs/starlight/schema'; |
| 191 | + import { blogSchema } from 'starlight-blog/schema' |
| 192 | +
|
| 193 | + export const collections = { |
| 194 | + docs: defineCollection({ schema: docsSchema({ extend: blogSchema() }) }), |
| 195 | + i18n: defineCollection({ type: 'data', schema: i18nSchema() }), |
| 196 | + }; |
| 197 | + ``` |
| 198 | +
|
| 199 | + 5. Now write your first blog post under `src/content/docs/blog` and see it show up in sidebar of blog. Update the link paths if needed. |
0 commit comments