-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-static.sh
More file actions
144 lines (128 loc) · 3.88 KB
/
Copy pathbuild-static.sh
File metadata and controls
144 lines (128 loc) · 3.88 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
140
141
142
143
144
#!/bin/bash
# Exit on error
set -e
echo "Starting static build process..."
# Clean existing build folders
rm -rf dist
mkdir -p dist/public
# Set environment to static for the build
export VITE_STATIC_DEPLOYMENT=true
# Build the frontend only using Vite
echo "Building frontend..."
npx vite build --outDir dist/public
# Copy sitemap.xml directly to ensure it's available and properly served
cp public/sitemap.xml dist/public/sitemap.xml
# Create a basic _redirects file for Netlify-compatible hosting
echo "/* /index.html 200" > dist/public/_redirects
# Make sure the sitemap isn't redirected by Netlify
echo "/sitemap.xml /sitemap.xml 200" >> dist/public/_redirects
# Create a basic netlify.toml file for better compatibility
cat > dist/public/netlify.toml << EOL
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
EOL
# Create a basic vercel.json file for Vercel compatibility
cat > dist/public/vercel.json << EOL
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
]
}
EOL
# Create a replit.json specifically for Replit static deployments
cat > dist/public/replit.json << EOL
{
"routing": {
"routes": [
{
"src": "/(.*)",
"dest": "/index.html"
}
]
}
}
EOL
# Create a basic now.json for legacy Now.sh compatibility
cat > dist/public/now.json << EOL
{
"version": 2,
"routes": [
{ "handle": "filesystem" },
{ "src": "/(.*)", "dest": "/index.html" }
]
}
EOL
# Create a web.config file for IIS/Azure hosting
cat > dist/public/web.config << EOL
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
EOL
# Create a 404.html that redirects to index.html (for GitHub Pages)
cat > dist/public/404.html << EOL
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<script>
// Single Page Apps for GitHub Pages
// https://github.com/rafgraph/spa-github-pages
// This script redirects to the main page for 404 errors on single page apps hosted on GitHub Pages
var pathSegmentsToKeep = 0;
var l = window.location;
l.replace(
l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '') +
l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/' +
l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~') +
(l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '') +
l.hash
);
</script>
</head>
<body>
Redirecting to the homepage...
</body>
</html>
EOL
# Add script to index.html to handle GitHub Pages redirects
if grep -q "GitHub Pages redirect script" dist/public/index.html; then
echo "GitHub Pages redirect script already exists in index.html"
else
sed -i '/<head>/a \
<!-- GitHub Pages redirect script -->\
<script type="text/javascript">\
(function(l) {\
if (l.search[1] === "/") {\
var decoded = l.search.slice(1).split("&").map(function(s) { \
return s.replace(/~and~/g, "&")\
}).join("?");\
window.history.replaceState(null, null,\
l.pathname.slice(0, -1) + decoded + l.hash\
);\
}\
}(window.location))\
</script>' dist/public/index.html || echo "Failed to insert GitHub Pages redirect script, continuing anyway"
fi
echo "Static build completed successfully!"
echo "The static website files are in the dist/public directory"
echo "These files are now ready for deployment to any static hosting service"