Skip to content

Commit

Permalink
local sitemap endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Resul Avan authored and Resul Avan committed Jun 10, 2020
1 parent 16a0811 commit 75fcf59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ See the [Features](#features) for more functionalities
- [x] lazy load (vue-lazyload)
- [x] crop with cropperjs
- [x] lightbox with buefy/bulma
- [x] hover button on profile photo
- [x] custom error page - simple
- [x] global notification
- [x] toaster notification
Expand Down
3 changes: 2 additions & 1 deletion src/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ const config: Configuration = {
baseURL: process.env.API_URL
},
serverMiddleware: [
'~/server/api'
'~/server/api',
'~/server/api/sitemap'
],

/*
Expand Down
1 change: 1 addition & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"nuxt-i18n": "^6.12.2",
"nuxt-property-decorator": "^2.7.2",
"rxjs": "^6.5.5",
"sitemap": "^6.1.5",
"slug": "^3.3.0",
"vee-validate": "^3.3.2",
"vue-cropperjs": "^4.1.0",
Expand Down
55 changes: 55 additions & 0 deletions src/server/api/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import express,{ Request, Response, Router } from 'express';
import { SitemapStream, streamToPromise } from 'sitemap';
import { createGzip } from 'zlib'

const service = '/sitemap.xml';

const staticRoutes = [
'/',
'/terms',
'/privacy-policy',
'/register',
'/login',
'/crop',
'/lightbox'
]

const app = express();
const router = Router();

let sitemap: Buffer | null = null;

router.get(service, (req: Request, res: Response) => {
console.log(req.originalUrl, ' called (get)');
res.header('Content-Type', 'application/xml');
res.header('Content-Encoding', 'gzip');

if (sitemap) {
res.send(sitemap)
return
}

try {
const smStream = new SitemapStream({ hostname: 'https://nuxt-ts-firebase-auth-ssr.web.app/' })
const pipeline = smStream.pipe(createGzip())

staticRoutes.forEach((route) => smStream.write({ url: route, changefreq: 'weekly', priority: 0.8 }))
smStream.end()

// cache the response
streamToPromise(pipeline).then((sm: Buffer) => sitemap = sm).catch((error:Error) => console.log(error))
// stream write the response
pipeline.pipe(res).on('error', (e: Error) => {
throw e
})
} catch (e) {
console.error(e)
res.status(500).end()
}
});

app.use(router)

export default {
handler: app
}

0 comments on commit 75fcf59

Please sign in to comment.