-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (124 loc) · 4.67 KB
/
Copy pathserver.js
File metadata and controls
139 lines (124 loc) · 4.67 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const fs = require('fs')
const path = require('path')
const express = require('express')
const axios = require('axios')
const { createServer: createViteServer } = require('vite')
const root = process.cwd()
const isProd = process.env.NODE_ENV === 'production'
const apiBase = process.env.VITE_URI || 'http://localhost:8000'
const templatePath = path.resolve(root, 'index.html')
const escapeHtml = (value) =>
value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
const replaceMeta = (html, tag, attr, value) => {
const regex = new RegExp(`<meta[^>]+${attr}="${tag}"[^>]*>`, 'i')
const replacement = `<meta ${attr}="${tag}" content="${escapeHtml(value)}">`
if (regex.test(html)) {
return html.replace(regex, replacement)
}
return html.replace('</head>', ` ${replacement}\n</head>`)
}
const replaceTitle = (html, title) => {
const escapedTitle = escapeHtml(title)
if (/<title>.*<\/title>/i.test(html)) {
return html.replace(/<title>.*<\/title>/i, `<title>${escapedTitle}</title>`)
}
return html.replace('</head>', ` <title>${escapedTitle}</title>\n</head>`)
}
const makeAbsoluteImage = (value) => {
if (!value) return null
if (value.startsWith('http://') || value.startsWith('https://')) return value
if (value.startsWith('//')) return `https:${value}`
if (value.startsWith('/')) return `https://shift7store.com${value}`
return `https://shift7store.com/${value}`
}
const injectProductMeta = async (html, product, pageUrl) => {
const title = product.name_en || product.name_ar || 'Shift7'
const description =
product.description_en || product.description_ar || product.sub_name_en || product.sub_name_ar || title
const imageSource =
product.key_default_image ||
product.media?.find((m) => m.name === 'product_main_image')?.url ||
product.media?.[0]?.url ||
'https://shift7store.com/og-image.png'
const image = makeAbsoluteImage(imageSource)
const url = pageUrl
let result = html
result = replaceTitle(result, `${title} | Shift7`)
result = replaceMeta(result, 'description', 'name', description)
result = replaceMeta(result, 'og:url', 'property', url)
result = replaceMeta(result, 'og:title', 'property', title)
result = replaceMeta(result, 'og:description', 'property', description)
result = replaceMeta(result, 'og:image', 'property', image)
result = replaceMeta(result, 'og:type', 'property', 'product')
result = replaceMeta(result, 'twitter:title', 'name', title)
result = replaceMeta(result, 'twitter:description', 'name', description)
result = replaceMeta(result, 'twitter:image', 'name', image)
return result
}
const createApp = async () => {
const app = express()
let vite
const getTemplate = async (url) => {
let template = fs.readFileSync(templatePath, 'utf-8')
if (!isProd && vite) {
template = await vite.transformIndexHtml(url, template)
}
return template
}
const renderProductPage = async (req, res) => {
try {
const template = await getTemplate(req.originalUrl)
const host = req.protocol + '://' + req.get('host')
const pageUrl = host + req.originalUrl
const response = await axios.get(`${apiBase}/api/home/product-details/${req.params.id}`)
const data = response.data
if (!data?.is_success || !data.data?.product) {
return res.status(404).send(template)
}
const html = await injectProductMeta(template, data.data.product, pageUrl)
res.set('Content-Type', 'text/html')
res.status(200).send(html)
} catch (error) {
console.error('Product meta injection failed:', error.message)
const template = await getTemplate(req.originalUrl)
res.status(200).send(template)
}
}
if (!isProd) {
vite = await createViteServer({
root,
server: {
middlewareMode: 'ssr',
watch: {
usePolling: true,
interval: 100,
},
},
})
app.use('/product-details/:id', renderProductPage)
app.use(vite.middlewares)
app.use('*', async (req, res) => {
const template = await getTemplate(req.originalUrl)
res.status(200).set({ 'Content-Type': 'text/html' }).send(template)
})
} else {
app.use('/product-details/:id', renderProductPage)
app.use(express.static(path.resolve(root, 'dist'), { index: false }))
app.use('*', async (req, res) => {
const template = await getTemplate(req.originalUrl)
res.status(200).set({ 'Content-Type': 'text/html' }).send(template)
})
}
return app
}
createApp().then((app) => {
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`)
})
})