You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -193,16 +192,16 @@ Load the page in the Mastro preview to see whether it still works!
193
192
Now you're almost ready to create that second page. Just one more thing to move to its own component file, because we want to reuse it: the skeleton of the page, often called `Layout`. Create a new file:
194
193
195
194
```js title=components/Layout.js
196
-
import { html } from'mastro/html.js';
197
-
import { Header } from'./Header.js';
198
-
import { Footer } from'./Footer.js';
195
+
import { html } from"mastro";
196
+
import { Header } from"./Header.js";
197
+
import { Footer } from"./Footer.js";
199
198
200
199
exportconstLayout= (props) =>
201
200
html`
202
201
<html>
203
202
<head>
204
203
<title>${props.title}</title>
205
-
<linkrel="stylesheet"href="styles.css">
204
+
<linkrel="stylesheet"href="/styles.css">
206
205
</head>
207
206
<body>
208
207
${Header()}
@@ -222,9 +221,8 @@ The above component is still just a function, but a function that takes one argu
222
221
Now you can reduce your `routes/index.server.js` file to:
223
222
224
223
```js title=routes/index.server.js
225
-
import { html } from'mastro/html.js';
226
-
import { htmlToResponse } from'mastro/routes.js';
227
-
import { Layout } from'../components/Layout.js';
224
+
import { html, htmlToResponse } from"mastro";
225
+
import { Layout } from"../components/Layout.js";
228
226
229
227
exportconstGET= () =>
230
228
htmlToResponse(
@@ -243,9 +241,8 @@ Note how we pass an object of the form `{ title, children }` as an argument to t
243
241
Now finally all that work pays off: add that second page by creating a new file:
Instead of changing our `routes/news.server.js` file in-place, first move it to a new folder `routes/news/` and rename it to `index.server.js`. This doesn't change anything, but we'll need the folder later for the detail pages anyway.
53
+
54
+
To list all your blog posts, change the index page to:
Note the use of the `readMarkdownFile` function that we imported from mastro. Because it needs to read the files from disk, it's an `async` function. That's why we need to `await` it, which in turn forces us to mark up our `GET` function as `async` as well.
72
80
73
-
Have a look at `/news` in the Mastro preview pane. Clicking one of the links will lead you to a page saying "404, page not found". That's because those pages don't exist yet. Create them by creating the file `routes/news/[slug].server.js`.
81
+
The `.slice(11, -3)` cuts off the first eleven and the last three character from the string: in our case it removes the leading `/data/posts` and the trailing `.md` from the filename.
82
+
83
+
Have a look at `/news` in the Mastro preview pane. Clicking one of the links will lead you to a page saying "404, page not found". That's because those pages don't exist yet.
74
84
75
-
The `[slug]` is a parameter. When your server receives a request for `/news/2024-01-30-hello-world`, the request will be routed to the `news/[slug].server.js` page and the `context.params.slug` variable will hold the value `2024-01-30-hello-world`.
Create the detail pages for the individual blog posts by creating the file `routes/news/[slug].server.js`.
89
+
90
+
The `[slug]` is a parameter. When your server receives an HTTP request for `/news/2024-01-30-hello-world`, the request will be routed to the `news/[slug].server.js` file. To read out the parameter, we use the `Request` object that is passed into our `GET` function, and the `getParams` helper function:
To read the markdown file from disk and convert the markdown to HTML, we use the asynchronous `readMarkdownFile` function.
109
+
94
110
Test following the links in the Mastro preview pane now. Congratulations, you have a working blog!
95
111
96
112
97
-
## Generating parametrized pages
113
+
## Generating the route parameters
98
114
99
-
To pre-generate all your html files, run `deno task build`. It will tell you that it generated the `out/index.html` page, but that `routes/news/[slug].server.js` is missing a `staticParams` field. That's because mastro can't magically guess all the blog post urls that we want to generate.
115
+
Try generating all your HTML files: click the **Generate** button in the top-right corner of the Mastro preview pane.
100
116
101
-
To let it know, we import and use the `htmlRoute` function, which takes two arguments:
117
+
It will generate the pages without parameters. But notice the error telling you that "/routes/news/[slug].server.js should export a function named getStaticPaths".
102
118
103
-
1. a configuration object (with the slugs to put into the paths to pre-generate), and
104
-
2. the function which we already had previously.
119
+
That's because Mastro cannot guess the paths for all the pages that we want to generate. In the preview (or on a running server) this works because the information is provided directly in the URL. But if we want to statically generate all the pages ahead of time, we need to tell Astro the paths of all our pages with route parameters. We do that by exporting a function called `getStaticPaths`, that returns an array of strings when called.
Now click **Generate** again. Then don't forget to [save your changes in the _Source Control_ tab](/guides/html/#save-changes-and-publish-to-the-web). Congratulations to your live blog!
0 commit comments