Skip to content

Commit 803d1ef

Browse files
Resul AvanResul Avan
authored andcommitted
sitemap function, robot.txt
1 parent 37014fb commit 803d1ef

File tree

7 files changed

+68
-2
lines changed

7 files changed

+68
-2
lines changed

firebase.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
"source": "/api/**",
2121
"function": "authApi"
2222
},
23+
{
24+
"source": "/sitemap.xml",
25+
"function": "sitemapApp"
26+
},
2327
{
2428
"source": "**",
2529
"function": "nuxtOnFunction"

functions/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
"nuxt-i18n": "^6.10.1",
3434
"nuxt-property-decorator": "^2.7.2",
3535
"nuxt-start": "^2.12.2",
36+
"sitemap": "^6.1.4",
3637
"slug": "^2.1.1",
3738
"vee-validate": "^3.3.0",
38-
"vuex-class": "^0.3.2"
39+
"vuex-class": "^0.3.2",
40+
"zlib": "^1.0.5"
3941
},
4042
"devDependencies": {
4143
"@nuxt/typescript-build": "^0.6.6",

functions/src/api/sitemap.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import express, { Request, Response, Router } from 'express';
2+
import { SitemapStream, streamToPromise } from 'sitemap';
3+
import { createGzip } from 'zlib'
4+
import { runWith } from "firebase-functions";
5+
import { runtimeOpts } from "../types";
6+
7+
const service = '/sitemap.xml';
8+
const app = express();
9+
const router = Router();
10+
11+
let sitemap: Buffer | null = null;
12+
13+
router.get(service, (req: Request, res: Response) => {
14+
console.log(req.originalUrl, ' called (get)');
15+
res.header('Content-Type', 'application/xml');
16+
res.header('Content-Encoding', 'gzip');
17+
18+
if (sitemap) {
19+
res.send(sitemap)
20+
return
21+
}
22+
23+
try {
24+
const smStream = new SitemapStream({ hostname: 'https://nuxt-ts-firebase-auth-ssr.web.app//' })
25+
const pipeline = smStream.pipe(createGzip())
26+
27+
smStream.write({ url: '/', changefreq: 'weekly', priority: 0.8 })
28+
smStream.write({ url: '/terms', changefreq: 'weekly', priority: 0.8 })
29+
smStream.write({ url: '/privacy-policy', changefreq: 'weekly', priority: 0.8 })
30+
smStream.write({ url: '/register', changefreq: 'weekly', priority: 0.8 })
31+
smStream.write({ url: '/login', changefreq: 'weekly', priority: 0.8 })
32+
smStream.end()
33+
34+
// cache the response
35+
streamToPromise(pipeline).then((sm: Buffer) => sitemap = sm).catch(error => console.log(error))
36+
// stream write the response
37+
pipeline.pipe(res).on('error', (e) => {
38+
throw e
39+
})
40+
} catch (e) {
41+
console.error(e)
42+
res.status(500).end()
43+
}
44+
});
45+
46+
app.use(router)
47+
48+
export const sitemapApp = runWith(runtimeOpts)
49+
.https
50+
.onRequest(app);

functions/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { nuxtOnFunction } from './nuxt-server'
22
export { authApi } from './api/authApi'
3+
export { sitemapApp } from './api/sitemap'

functions/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"target": "es2018",
1010
"allowSyntheticDefaultImports": true,
1111
"resolveJsonModule": true,
12-
"esModuleInterop": true
12+
"esModuleInterop": true,
13+
"types": [
14+
"sitemap"
15+
]
1316
},
1417
"compileOnSave": true,
1518
"include": [

src/nuxt.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export default {
1414
hid: 'description',
1515
name: 'description',
1616
content: process.env.npm_package_description || ''
17+
},
18+
{
19+
name: "google-site-verification",
20+
content: "7KSGr_zujhmsc2fhu0iAfR_L0e5--J_QwD5EeePB1yM"
1721
}
1822
],
1923
link: [

src/static/robots.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow: /admin

0 commit comments

Comments
 (0)