Skip to content

Instantly share code, notes, and snippets.

View MaikelVeen's full-sized avatar

Maikel Veen MaikelVeen

View GitHub Profile
@MaikelVeen
MaikelVeen / sitemap-example.xml
Created December 27, 2020 20:12
Sitemap example
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>
```
.
├── _config.yml
├── _data
│ └── members.yml
├── _drafts
│ ├── begin-with-the-crazy-ideas.md
│ └── on-simplicity-in-technology.md
├── _includes
│ ├── footer.html
@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;
@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 / 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 / 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 / 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 / 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 / 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 / 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>