Skip to content

Instantly share code, notes, and snippets.

View MaikelVeen's full-sized avatar

Maikel Veen MaikelVeen

View GitHub Profile
@MaikelVeen
MaikelVeen / microdata-breadcrumb.html
Created January 20, 2021 22:32
Microdata to support breadcrumb
<ol itemscope itemtype="https://schema.org/BreadcrumbList">
<li itemprop="itemListElement" itemscope
itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/books">
<span itemprop="name">Books</span></a>
<meta itemprop="position" content="1" />
</li>
<li itemprop="itemListElement" itemscope
itemtype="https://schema.org/ListItem">
@MaikelVeen
MaikelVeen / movie-microdata.html
Created January 20, 2021 22:09
Simple microdata example
<div itemscope itemtype ="http://schema.org/Movie">
<h1 itemprop="name">Avatar</h1>
<span>Director: <span itemprop="director">James Cameron</span> (born August 16, 1954)</span>
<span itemprop="genre">Science fiction</span>
<a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
</div>
@MaikelVeen
MaikelVeen / GetPathsFromBuildFolder.tsx
Last active January 10, 2021 00:31
GetPathsFromBuildFolder
const GetPathsFromBuildFolder = (dir: string, urlList: Array<Url>, host: string, basePath: string): Array<Url> => {
const files: string[] = fs.readdirSync(dir);
urlList = urlList || [];
files.forEach((file) => {
if (fs.statSync(dir + file).isDirectory()) {
urlList = GetPathsFromBuildFolder(dir + file + '/', urlList, host, basePath);
} else {
if (path.extname(file) == '.json') {
let route = path.join(dir + file.substring(0, file.length - 5));
@MaikelVeen
MaikelVeen / getPathsFromManifest-full.tsx
Last active January 9, 2021 23:31
Getting the paths from the manifest including dynamic routes
const GetPathsFromManifest = (manifest: any, basePath: string, host: string): Array<Url> => {
let routes: Array<string> = [];
for (let [route, file] of Object.entries(manifest)) {
if (!isNextInternalUrl(route)) {
// Add static paths
routes = routes.concat(route);
} else if (isDynamicUrl(route)) {
// Add dynamic paths
const dynamicRoute: DynamicRoute = GetBuildPath(route, basePath);
@MaikelVeen
MaikelVeen / next.config.js
Created January 9, 2021 21:34
Fixing the client bundle build
module.exports = {
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
if (!isServer) {
config.node = {
fs: 'empty'
}
}
return config
@MaikelVeen
MaikelVeen / isNextInternalUrl.tsx
Created December 28, 2020 20:42
Check wether or not a route in next.js is an internal route
const isNextInternalUrl = (path: string): boolean => {
return new RegExp(/[^\/]*^.[_]|(?:\[)/g).test(path);
}
@MaikelVeen
MaikelVeen / getPathsFromManifest.tsx
Last active January 9, 2021 22:06
Getting the paths from the manifest object
const GetPathsFromManifest = (manifest: any, basePath: string, host: string): Array<Url> => {
let routes: Array<string> = [];
for (let [route, file] of Object.entries(manifest)) {
if (!isNextInternalUrl(route)) {
// Add static paths
routes = routes.concat(route);
}
}
@MaikelVeen
MaikelVeen / readmanifest.tsx
Last active January 9, 2021 21:54
Reading the manifest file.
const ReadManifestFile = (basePath: string): object => {
const routes_manifest_path = path.join(basePath + '/.next/server/pages-manifest.json');
// Read from the file
if (fs.existsSync(routes_manifest_path)) {
const raw_json = fs.readFileSync(routes_manifest_path);
return JSON.parse(raw_json.toString());
} else return null;
};
@MaikelVeen
MaikelVeen / getServerSideProps.tsx
Last active February 28, 2021 11:18
Sitemap generation GetServerSideProps
import { GetServerSideProps } from 'next';
import fs from 'fs';
import path from 'path';
export default function Sitemap() {}
type Url = {
host: string;
route: string;
date?: Date;
```
.
├── _config.yml
├── _data
│ └── members.yml
├── _drafts
│ ├── begin-with-the-crazy-ideas.md
│ └── on-simplicity-in-technology.md
├── _includes
│ ├── footer.html