Skip to content

Instantly share code, notes, and snippets.

View emilpriver's full-sized avatar
🏠
Coding

Emil Priver emilpriver

🏠
Coding
View GitHub Profile
@emilpriver
emilpriver / sitemap.ts
Last active October 20, 2021 15:41
Sitemap nextjs code
// import functions from the package
⁠import { SitemapStream, streamToPromise } from "sitemap";
⁠// Fetch data from a source which will be used to render the sitemap.
⁠const { posts } = await graphlqlFetch(`
⁠ query getSitemapData {
⁠ projects: allWorks {
⁠ slug {
⁠ current
⁠ }
⁠ publishedAt
// import functions from the package
import { SitemapStream, streamToPromise } from "sitemap";
// A custom function I use to fetch data from a backend. I will keep the import to make it more clear why "graphlqlFetch" is used in the code
import graphlqlFetch from "lib/apollo"
export default async (req, res) => {
// Fetch data from a source which will be used to render the sitemap.
const { posts } = await graphlqlFetch(`
query getSitemapData {
@emilpriver
emilpriver / sitemap.xml.js
Created February 3, 2020 19:26
NextJS Sitemap example using Nextjs SSR.
import React from "react";
import axios from "axios";
const sitemapXml = data => {
let latestPost = 0;
let projectsXML = "";
data.map(post => {
const postDate = Date.parse(post.modified);
if (!latestPost || postDate > latestPost) {
@emilpriver
emilpriver / gist:0057b83980e8f0ae5a87d99859a7d66a
Created January 20, 2020 12:43
Write getStaticProps function
## Example 1:
export async function getStaticProps() {
const data = await axios
.get("https://api.priver.dev/wp-json/wp/v2/works?filter=[orderby]=date")
.then(d => d.data);
console.log(data);
return { props: { projects: data } };
}
class Home extends React.Component {