-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
88 lines (80 loc) · 3.4 KB
/
vite.config.ts
File metadata and controls
88 lines (80 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import path from 'path'
import fs from 'fs'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import type { Plugin } from 'vite'
// Keeps coi-serviceworker.js in sync with node_modules on every build
function coiServiceworkerPlugin(): Plugin {
return {
name: 'coi-serviceworker',
buildStart() {
const src = path.resolve(__dirname, 'node_modules/coi-serviceworker/coi-serviceworker.min.js')
const dest = path.resolve(__dirname, 'public/coi-serviceworker.js')
let content = fs.readFileSync(src, 'utf-8')
// Skip cross-origin navigate requests (e.g. iframes to YouTube) so the
// browser handles them directly instead of routing through the SW fetch.
// Only intercept same-origin requests — cross-origin resources (YouTube iframes,
// analytics, CDNs) must be left alone so the browser handles them natively.
content = content.replace(
'if("only-if-cached"===r.cache&&"same-origin"!==r.mode)return;',
'if(!r.url.startsWith(self.location.origin))return;if("only-if-cached"===r.cache&&"same-origin"!==r.mode)return;',
)
fs.writeFileSync(dest, content, 'utf-8')
},
}
}
// Generates dist/sitemap.xml at build time from static + dynamic route IDs
function sitemapPlugin(): Plugin {
return {
name: 'sitemap',
closeBundle() {
const { challengeIds, problemIds } = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'src/data/sitemap-ids.json'), 'utf-8'),
) as { challengeIds: string[]; problemIds: string[] }
const BASE_URL = 'https://react.ingeniousclan.com'
const today = new Date().toISOString().split('T')[0]
const staticRoutes = [
{ path: '/', priority: '1.0', changefreq: 'weekly' },
{ path: '/frontend-coding', priority: '0.9', changefreq: 'weekly' },
{ path: '/js-problems', priority: '0.9', changefreq: 'weekly' },
{ path: '/jobs/chennai', priority: '0.7', changefreq: 'monthly' },
{ path: '/about', priority: '0.4', changefreq: 'monthly' },
]
const urls = [
...staticRoutes.map((r) => ({ url: `${BASE_URL}${r.path}`, priority: r.priority, changefreq: r.changefreq })),
...challengeIds.map((id) => ({ url: `${BASE_URL}/frontend-coding/${id}`, priority: '0.8', changefreq: 'monthly' })),
...problemIds.map((id) => ({ url: `${BASE_URL}/js-problems/${id}`, priority: '0.8', changefreq: 'monthly' })),
]
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map(({ url, priority, changefreq }) => ` <url>
<loc>${url}</loc>
<lastmod>${today}</lastmod>
<changefreq>${changefreq}</changefreq>
<priority>${priority}</priority>
</url>`).join('\n')}
</urlset>`
const outDir = path.resolve(__dirname, 'dist')
if (fs.existsSync(outDir)) {
fs.writeFileSync(path.resolve(outDir, 'sitemap.xml'), xml, 'utf-8')
console.log(`[sitemap] Generated ${urls.length} URLs → dist/sitemap.xml`)
}
},
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), tailwindcss(), coiServiceworkerPlugin(), sitemapPlugin()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
headers: {
'Cross-Origin-Embedder-Policy': 'credentialless',
'Cross-Origin-Opener-Policy': 'same-origin',
},
},
})