Skip to content

Commit bb1aecb

Browse files
committed
Better text
1 parent 02e795c commit bb1aecb

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# The Mastro Guide 👨‍🍳
22

3+
Repo for https://mastrojs.github.io
4+
35
Because of the syntax highlighting and other niceties that are already built in, we're currently using [Starlight](https://starlight.astro.build) for this website.
46

57
Starlight looks for `.md` or `.mdx` files in the `src/content/docs/` directory. Each file is exposed as a route based on its file name.

src/content/docs/guides/javascript.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ Now that you got a taste of HTML and CSS, let's talk about JavaScript. It's the
88

99
## Client-side and server-side JavaScript
1010

11-
Inside the browser, JavaScript is best used sparingly to add interactivy to a page. Although unfortunately, some websites load millions of lines of JavaScript into the browser. Since a browser is also known as a _client_, this is known as client-side JavaScript. In fact, client-side JavaScript frameworks like React compute the whole page in the browser of your user every time they load the page. This may be required for an app like Google Docs or Visual Studio Code. But for most websites, it just makes everything slow for no good reason – especially on mobile phones, with their limited computing power and slow networks.
11+
Inside the browser, JavaScript is best used sparingly to add interactivy to a page. Although unfortunately, some websites load millions of lines of JavaScript into the browser. Since a browser is also known as a _client_, this is known as client-side JavaScript. In fact, client-side JavaScript frameworks like React compute the whole page in the browser of your user every time they load the page. This may be required for an app like Google Docs or Visual Studio Code. But for most websites, it just makes everything slow for little reason – especially on mobile phones, with their limited computing power and slow networks.
12+
13+
A lot of these "websites" even go so far as to take control of loading the next page away from the browser, and reimplement the functionality in JavaScript. This approach is called a Single-Page-App (SPA), and just recently [peaked in popularity](https://nolanlawson.com/2022/05/21/the-balance-has-shifted-away-from-spas/). Nowadays, you can get equally smooth page transitions by adding [two lines of CSS](https://webkit.org/blog/16967/two-lines-of-cross-document-view-transitions-code-you-can-use-on-every-website-today/) to your MPA (Multi-Page-App).
1214

1315
However, JavaScript is a general-purpose programming language like any other. Nowadays, you can run JavaScript also on your server or laptop, generate the HTML there, and only send that to your user's browser – just like people have done with PHP or Ruby for ages. This is known as server-side JavaScript and it's what we'll be doing primarily in this guide.
1416

1517

16-
## Introducing JavaScript
18+
## Getting your feet wet with JavaScript
19+
20+
However, one of the easiest way to mess around with JavaScript is still your browser.
1721

1822
On your published website, open your [browser's developer tools](https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools#how_to_open_the_devtools_in_your_browser) again. But this time, switch to the tab named **Console**.
1923

src/content/docs/guides/multiple-pages-with-shared-components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Now, to `import` the two functions we just created, you first need to convert th
5858

5959
### JavaScript page handlers
6060

61+
All files in the `routes/` folder are sent out unmodified to your website's visitors – except for JavaScript files ending in `.server.js` or `.server.ts`. The code in these files is run and the result is sent to your website's visitors.
62+
6163
Rename the `routes/index.html` file to `routes/index.server.js` and change its contents to:
6264

6365
```js title=routes/index.server.js ins={1-8,15,22,25}

src/content/docs/guides/rest.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ title: Rest
33
template: splash
44
---
55

6-
All files in the `routes/` folder are sent out unmodified to your website's visitors – except for JavaScript files ending in `.server.js` or `.server.ts`. The code in these files is run and the result is sent to your website's visitors.
7-
8-
96
## Images
107

118
Now that you have your basic blog up and running on a live website, let's add some images.

src/content/docs/guides/static-blog-from-markdown-files.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ export const GET = async () => {
7979
};
8080
```
8181

82-
Note the use of the `readMarkdownFiles` function that we imported from mastro. Because it requests the files from the computer's harddisk (which is potentially something we need to wait for), we need to `await` it. This in turn forces us to mark up our `GET` function as `async`.
82+
Note the `../../` when importing the `Layout` component, while previously it was just `../`. That's because this file is in the `news/` folder, so you need to go one level further up than before to get to the `components/` folder.
8383

84-
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.
84+
The code imports the `readMarkdownFiles` function from mastro. Because that function requests the files from the computer's harddisk (which might take some time), we need to `await` it. This in turn forces us to mark up our `GET` function as `async` (short for [asynchronous](https://eloquentjavascript.net/11_async.html)).
85+
86+
The `.slice(11, -3)` [slices](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) 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.
8587

8688
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.
8789

@@ -116,8 +118,31 @@ To read the markdown file from disk and convert the markdown to HTML, we use the
116118

117119
Test following the links in the Mastro preview pane now. Congratulations, you have a working blog!
118120

121+
## Debugging with `console.log`
122+
123+
Usually when programming, thinks go wrong at some point. It's fairly uncommon for a programmer to write out a program and get exactly what they wanted the first time they run it. Perhaps you get a syntax error and the program doesn't even start running. Or perhaps the JavaScript syntax was correct, but the program crashes later on, or at least doesn't do what you wanted it to. In this second case, it can be useful to add `console.log` statements in various places of your code and see what it prints into your browser's JavaScript console (what you used in a [previous chapter](/guides/javascript/#getting-your-feet-wet-with-javascript)). Give it a try:
124+
125+
```js title=routes/news/[slug].server.js ins={6}
126+
import { getParams, htmlToResponse, readMarkdownFile } from "mastro";
127+
import { Layout } from "../../components/Layout.js";
128+
129+
export const GET = async (req) => {
130+
const { slug } = getParams(req.url);
131+
console.log('the slug variable holds the value', slug)
132+
const post = await readMarkdownFile(`data/posts/${slug}.md`);
133+
return htmlToResponse(
134+
Layout({
135+
title: post.meta.title,
136+
children: post.content,
137+
})
138+
);
139+
}
140+
```
141+
142+
After opening your browser's JavaScript console and clearing what's already printed out there with the button labelled `🚫`, open the page in the Mastro preview pane.
143+
119144

120-
## Generating the route parameters
145+
## Generating pages with route parameters
121146

122147
Try generating all your HTML files: click the **Generate** button in the top-right corner of the Mastro preview pane.
123148

0 commit comments

Comments
 (0)