Hi ๐
This is the first issue that includes a Nuxt tip, you can expect more of them in upcoming issues.
This week is my lightning talk at Vue.js Nation 2023, it's scheduled for 16:40 (Europe/Berlin timezone) on January 25th.
Have a nice week โ๏ธ
Nuxt Tip: Adding a Latest Route
Utilizing
Let's take a look at the route middleware's code:
export default defineNuxtRouteMiddleware(async (to, from) => {
if (to.fullPath === '/blog/latest') {
const data = await queryContent('blog').where({ _partial: false }).sort({ date: -1 }).findOne()
navigateTo(data._path)
}
})
Route middleware are navigation guards that receive the current route and the next route as arguments. First, you need to check if the fullPath
of the current route equals the expected latest route.
Next, we use sort({ date: -1 })
query.
Finally, we use the navigateTo
function, which is globally available in Nuxt and redirects to the given route.
Want to give it a try? Try navigating to
Curated Vue Content
๐ Use Shiki to Style Code Blocks in HTML Emails |
๐๐ป In this article, I'll explain how I use Shiki in Nuxt 3 & Nuxt Content to generate nicely styled code blocks in my newsletter emails. |
mokkapps.de |
๐ Server Routes in Nuxt 3 |
๐๐ป Nuxt 3 not only does it give us amazing tools to make building our frontend radically easier, it also gives us some awesome tools for building our backend. |
masteringnuxt.com |
๐ How to Build Feature Rich Forms in Vue.js |
๐๐ป In this article, Daniel explains why FormKit is his go to solution for building forms in Vue. |
vueschool.io |
๐ Start Testing With Vitest (Beginnerโs Guide) |
๐๐ป This beginner's guide will teach you how to set up your testing environment, write and run tests, and utilize the features of Vitest for testing your Vue components. |
vueschool.io |
Quote of the week
JavaScript Tip: Return Object Literal From an Arrow Function
ECMAScript 2015 introduced the use of arrow functions, which offer a concise syntax for defining functions without the need for the function
keyword.
An example of this shorthand syntax can be seen in a function that doubles a given integer:
const doubleCount = (value) => {
return {
doubleCount: value * 2
}
}
You might be tempted to simplify the code by removing the return
statement, like this:
const doubleCount = value => {
doubleCount: value * 2
}
Upon calling the doubleCount
function, it is observed that it is not functioning as intended. Regardless of the input value passed, the function returns undefined
. Why is that?
The problem with the arrow function is that it's not returning the expected value because the parser is treating the braces as a block statement instead of an object literal. As a result, the parser is interpreting the label doubleCount
as belonging to the expression statement value * 2
and there is no return statement, resulting in the function returning undefined
.
To correct this behavior, you must ensure that the parser interprets the object literal as an expression instead of a block statement. This can be achieved by adding parentheses around the entire body of the function:
const doubleCount = value => ({
doubleCount: value * 2
})
Curated Web Development Content
๐ State of JavaScript 2022 has been released |
๐๐ป Each year thousands of devs are surveyed to find out what the most popular parts of the JavaScript world are, from language features to frameworks. |
2022.stateofjs.com |
๐ Draw SVG rope using JavaScript | ||
| ||
muffinman.io |
๐ Draw SVG rope using JavaScript |
๐๐ป Stanko takes us through the process he came up with in regard to transforming an SVG path into a vector rope drawing. |
www.scottantipa.com |
๐ ๏ธ RippleUI |
๐๐ป A nice collection of 20+ customizable components with support for dark mode out-of-the-box. Includes good documentation and examples for each component. |
www.ripple-ui.com |
๐ ๏ธ Pure CSS |
๐๐ป This website contains a set of small CSS modules that can be used in any web project. |
purecss.io |
๐ฎ Live coding a custom JavaScript runtime |
๐๐ป Luca wrote a custom JavaScript runtime in 30 minutes live on stage during his talk at Armada JS Conf 2022 |
twitter.com |