Nue.js Tutorial: Building a simple Web App
Table of Contents
nue.js
Introduction
Nue is a web framework that emphasizes standards to build content-focused websites. It's relatively new in the market and is already seen as a potential successor to popular frameworks that are bundled with complexity and endless fixes. Given its rapidly rising popularity, understanding it is of great interest to developers.
Understanding the Nue.js directory structure is key to effective development and maintenance. Let’s delve into the specific roles and contents of each file and folder within a typical Nue.js project.
Type of files in Nue.js
- .md (Markdown Files): Used for content creation, allowing easy writing and editing of textual content.
- .css (Cascading Style Sheets): Define the visual presentation of your content.
- .html (HyperText Markup Language): Structure the content and layout of web pages.
- .dhtml (Dynamic HTML): Group-related components, functioning as mini-libraries.
- .yaml (YAML Ain't Markup Language): Store configuration and metadata in a human-readable format.
- Interactive islands: An interactive island is a small, self-contained piece of interactivity added to a mostly static webpage. For example, imagine a webpage that is mostly static. If we want to add a form (an interactive element), we would prefer to place it separately from the static content. We would code it inside a .dhtml file and then reference it from the static page. The dynamic .dhtml file is what we refer to as an interactive island.
Directory Structure
Here we will first outline the directory structure:
Root directory
Root directory directory that acts as the base of your application.
- index.md: This is a markdown file that represents the homepage.
- site.yaml This is a YAML file that contains application-wide settings.
@global directory
This directory houses styles and layout files that apply site-wide. it contains files such as:
- layout.css: contains core layout styles that structure your site
- layout.html: contains header and footer templates to maintain a consistent navigation experience.
- typography.css: contains CSS styles for typography.
blog directory
The blog directory is dedicated to blog-related content, assets, and templates. It's important to note that other directories in a Nue.js project follow a similar structure to the blog directory. For this reason, we’ve used the blog directory as an example to explain the layout. Essentially, each folder corresponds to a specific URL path on your site, and its name typically forms part of the URL structure starting from the root. This consistency helps maintain an organized, intuitive structure across your project.
- blog.yaml: holds blog-specific settings such as content collection name and included styles.
- hero.html defines the header template for the blog posts, incorporating metadata like title, publication date, and author.
@library
Contains modular CSS files for reusable components.
assets directory
Directory for hierarchically storing your assets.
.dist
The output directory is where everything is collected after compilation and ready for deployment.
Examples of Nue.js Code
In this section, we will explore how to write code using the files listed above. The source code we will be using can be found on github.(https://github.com/nuejs/nue/tree/master/packages/examples/simple-blog) or by running:
nue create simple-blog
Once you have the code up and running, come back here, and let’s go over the code. Let's discuss the most important files in the code.
site.yaml
# yaml-language-server: $schema=https://json.schemastore.org/nuejs-site.json
globals: ["@global"]
libs: ["@library"]
view_transitions: true
native_css_nesting: true
inline_css: true
sections: true
title_template: "Emma Bennet / %s"
og_image: /img/og_emma.png
author: Emma Bennet
favicon: /img/favicon.jpg
description: Minimalist, designer, and UX developer
port: 8083
# OG image
origin: https://simple-blog.nuejs.org
og: /img/og.jpg
navigation:
header:
- Emma Bennet: /
- Contact: /contact/
- Media: /media
footer:
- © Emma Bennet: /
- image: /img/twitter.svg
url: //x.com/tipiirai
alt: Twitter (X) profile
size: 22 x 22
- image: /img/github.svg
url: //github.com/nuejs/nue/
alt: Github Projects
size: 22 x 22
- image: /img/linkedin.svg
url: //linkedin.com/in/tipiirai
alt: LinkedIn profile
size: 22 x 22
Let’s begin by taking a look at a sample site.yaml file. Its located at the root directory . In it, we define application-wide settings.
The global key-value pair is used to include the global styles from the @global directory. This includes .css files for things like layout and typography.
This is followed by several SEO-related key-value pairs, which include:
- title_template: Defines how the title will appear.
- og_image: Specifies the location of the image, which is important for social sharing (e.g., on Twitter).
- author: Metadata that gives the name of the author of the blog.
- favicon: Specifies the location of the favicon, which appears in the browser's menu bar.
- description: Provides the meta description of the blog page, useful for SEO purposes.
- port: A server directive that specifies which port to use for serving the site.
- navigation: Defines an array (you can look up arrays in YAML) containing two objects: header and footer. Each object contains an array of text and accompanying links. Think of this as the data required by the website for generating navigation elements.
layout.html
<header>
<navi :items="navigation.header"/>
</header>
<footer>
<navi :items="navigation.footer"/>
</footer>
layout. HTML defines the structure of the website. Mostly it provides a header and footer structure. It's situated at '@global/layout.html'. It references the data (arrays) we defined above on the site.YAML. This way we reuse the data if it's needed elsewhere.
The rest of the files in the @global are just a bunch of CSS files.
blog.yaml
This file helps us include CSS modules from either @global or @library.
The code below includes content.css, cards.css and motino.css into the blog page:
include: [ content, cards, motion ]
.markdown files
These are files that are written by content writers using markdown. We will only focus on the front matter. Front matter is like meta data for the blog. Lets see an example:
---
title: Practical strategies for using colors in CSS
credits: BalkanBrothers
thumb: img/colors-thumb.png
og: img/colors-2.jpg
date: 2024-11-23
---
The above is metadata for the blog. It's given just before starting out the blog itself and in the above format.
hero.html
This file describes the structure of the blog's hero section ( the title, dates, and other data above the blog). It's templated using curly braces '{}' so that it can be reused by fetching that template data from the blog's metadata markdown shown above.
how blogs work in Nue.js
- Folder Name: The name of the blog folder forms the first part of the URL. This makes the folder name important for SEO and URL organization. For example, if the folder is named tech, the URL will start with yourdomain.com/tech.
- Markdown Files: Inside the folder, each .md file represents a single blog post. The name of each Markdown file will become the path of the URL. For example, if there is a file called first-post.md, the URL for that blog post will be yourdomain.com/tech/first-post.
- hero.html: The hero.html file inside the blog folder is a template used to define the header for all the posts in that folder. It includes meta-information like the post title, publication date, author, and featured image. This template is dynamically populated for each post based on the metadata provided in the Markdown files.
Conclusion.
Nue.js is a simple and efficient web framework that promotes a clear separation of concerns, allowing content creators and developers to have well-defined roles. It simplifies the development process by minimizing complexities commonly found in other web frameworks. With the guidance provided in this tutorial, you now have a clear understanding of how to set up and code a basic Nue.js web application. Happy coding!
Frequently Asked Questions
What is Nue.js used for?
Nue.js is a lightweight web framework designed for building content-first websites. It promotes simplicity by separating content, structure, and styling, making it ideal for developers and content creators who want minimal configuration and maximum performance.
What file types does Nue.js support?
Nue.js supports several file types, including .md (Markdown) for content, .css for styles, .html for layout templates, .dhtml for reusable dynamic components, and .yaml for configuration and metadata.
What is an interactive island in Nue.js?
An interactive island is a small, dynamic element—like a form—placed inside a .dhtml file and referenced from a mostly static page. This allows developers to keep interactivity modular and separate from static content.
How do I structure a Nue.js project?
A typical Nue.js project includes directories like @global (for global styles and layout), @library (for reusable CSS components), route folders (like /blog), and configuration files like site.yaml. Each route folder mirrors a URL path on your site.
What is the purpose of site.yaml in Nue.js?
site.yaml stores global configuration such as navigation links, metadata (title, description, author), and global style imports. It’s key to maintaining consistent data across your entire site.
Can I use Markdown to create pages in Nue.js?
Yes. Markdown (.md) files are used to write content like blog posts. Each file includes front matter metadata and is rendered using layout templates like hero.html.
How do blog posts work in Nue.js?
Blog posts are placed inside a folder (e.g., /blog) as .md files. Each file represents one blog post, and the folder name becomes part of the URL path. The hero.html file defines the header layout using metadata from each post.
How is Nue.js different from traditional web frameworks?
Unlike traditional frameworks that bundle JavaScript-heavy tooling, Nue.js focuses on standard web technologies like HTML, CSS, and Markdown. It reduces complexity, improves performance, and makes content the primary concern.
Is Nue.js suitable for SEO?
Yes. Nue.js supports clean URLs, metadata in YAML, and reusable layout structures—making it ideal for building SEO-friendly, content-rich websites.
References
Background References
- (January 9, 2025). Nue.js Official Site. *Nue.js*. Retrieved January 9, 2025 from https://nuejs.org
- (January 16, 2025). MDN Web Docs (HTML/CSS/Markdown references) . *MDN*. Retrieved January 16, 2025 from https://developer.mozilla.org
- (November 21, 2024). YAML Spec Documentation . *YAML Spec Documentation *. Retrieved November 21, 2024 from https://yaml.org
About the Author
Joseph Horace
Horace is a dedicated software developer with a deep passion for technology and problem-solving. With years of experience in developing robust and scalable applications, Horace specializes in building user-friendly solutions using cutting-edge technologies. His expertise spans across multiple areas of software development, with a focus on delivering high-quality code and seamless user experiences. Horace believes in continuous learning and enjoys sharing insights with the community through contributions and collaborations. When not coding, he enjoys exploring new technologies and staying updated on industry trends.