Skip to content

Commit 2321d6b

Browse files
committed
fix: /config endpoint resolving
1 parent d033817 commit 2321d6b

File tree

10 files changed

+39
-21
lines changed

10 files changed

+39
-21
lines changed

app.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,27 @@ async function initializeApp () {
7171
try {
7272
const vite = require('vite')
7373
viteServer = await vite.createServer({
74-
server: { middlewareMode: true },
74+
server: {
75+
middlewareMode: true,
76+
fs: {
77+
strict: true, // Strict file serving
78+
allow: [__dirname] // Only allow serving from the project root
79+
}
80+
},
7581
appType: 'custom',
76-
root: __dirname, // Ensure root is set
77-
base: '/'
82+
root: __dirname,
83+
base: '/.vite/' // Set custom base path for Vite assets
7884
})
79-
// Use vite's connect instance as middleware *before* anything else
80-
app.use(viteServer.middlewares)
81-
logger.info('Vite middleware enabled.')
85+
86+
// Only use Vite middleware for paths that start with /.vite/
87+
app.use((req, res, next) => {
88+
if (req.path.startsWith('/.vite/')) {
89+
return viteServer.middlewares(req, res, next)
90+
}
91+
next()
92+
})
93+
94+
logger.info('Vite middleware enabled for /.vite/ path only.')
8295
} catch (e) {
8396
logger.error('Failed to create Vite server:', e)
8497
process.exit(1)

lib/vite-helpers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ function getViteAssets (entryName) {
6868
console.warn(`Vite manifest entry not found for: ${entryKey}`)
6969
}
7070
} else {
71-
// In development, Vite handles everything through its dev server
72-
assets.js.push(`/@vite/client`) // Vite HMR client
73-
// Request the actual file path relative to the project root
74-
assets.js.push(`/public/js/${entryName}.js`)
71+
// In development, Vite now uses a custom base path
72+
assets.js.push(`/.vite/@vite/client`) // Vite HMR client with custom base
73+
// Request the actual file path relative to the project root with the custom base
74+
assets.js.push(`/.vite/public/js/${entryName}.js`)
7575
}
7676

7777
return assets

public/js/extra.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import Prism from 'prismjs'
55
import hljs from 'highlight.js'
6-
import * as PDFObject from 'pdfobject'
6+
import PDFObject from 'pdfobject'
77
import { saveAs } from 'file-saver'
88

99
import escapeHTML from 'lodash/escape'

public/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ import '@hackmd/ot/lib/undo-manager'
102102
// import '@hackmd/ot/lib/client'
103103
import '@hackmd/ot/lib/editor-client'
104104

105-
import * as mousewhell from 'jquery-mousewheel'
105+
import mousewhell from 'jquery-mousewheel'
106106
import '@vendor/jquery-textcomplete/jquery.textcomplete'
107107
import '@vendor/jquery-ui/jquery-ui.min.js'
108108
import '@vendor/bootstrap/tooltip.min.js'

public/js/lib/editor/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ import debounce from 'lodash/debounce'
7070

7171
import * as utils from './utils'
7272
import config from './config'
73-
import statusBarTemplate from './statusbar.html'
74-
import toolBarTemplate from './toolbar.html'
73+
import statusBarTemplate from './statusbar.html?raw'
74+
import toolBarTemplate from './toolbar.html?raw'
7575
import { linterOptions } from './markdown-lint'
7676
import CodeMirrorSpellChecker, { supportLanguages, supportLanguageCodes } from './spellcheck'
7777
import { initTableEditor } from './table-editor'

public/views/codimd/foot.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
<!-- <%- include('../build/index-pack-scripts') %> -->
3434
<% } %>
3535

36+
<script src="<%- serverURL %>/config"></script>
3637
<%- generateTags(viteAssets('index')).jsTags %>

public/views/index/foot.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
<%# Old webpack includes removed %>
1313
<% } %>
1414
<%# Inject Vite JS assets for the 'index' entry point %>
15+
<script src="<%- serverURL %>/config"></script>
1516
<%- generateTags(viteAssets('cover')).jsTags %>

public/views/pretty.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,6 @@
114114
<%# Old webpack includes removed %>
115115
<% } %>
116116
<%# Inject Vite JS assets for the 'pretty' entry point %>
117+
<script src="<%- serverURL %>/config"></script>
117118
<%- generateTags(viteAssets('pretty')).jsTags %>
118119
<%- include('shared/ga') %>

public/views/slide.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<%# Old webpack includes removed %>
126126
<% } %>
127127
<%# Inject Vite JS assets for the 'slide' entry point %>
128+
<script src="<%- serverURL %>/config"></script>
128129
<%- generateTags(viteAssets('slide')).jsTags %>
129130
</body>
130131
</html>

vite.config.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export default defineConfig({
2525
// Add exclude for HTML files to prevent inject plugin from processing them
2626
exclude: ['**/*.html', '**/*.css']
2727
}),
28-
string({
29-
include: '**/*.html'
30-
}),
28+
// string({
29+
// include: '**/*.html'
30+
// }),
3131
copy({
3232
targets: [
3333
{
@@ -143,7 +143,7 @@ export default defineConfig({
143143
ot: {} // Define ot as empty object for browser compatibility
144144
},
145145
root: __dirname,
146-
base: '/',
146+
base: '/.vite/', // Updated to match our custom base path in app.js
147147
publicDir: false, // Let Vite handle all assets through middleware
148148
appType: 'custom',
149149
optimizeDeps: {
@@ -166,14 +166,15 @@ export default defineConfig({
166166
ws: true,
167167
hmr: {
168168
protocol: 'ws',
169-
host: 'localhost'
169+
host: 'localhost',
170+
path: '/.vite/hmr' // Specify a custom HMR path
170171
},
171172
fs: {
173+
strict: true, // Only serve explicitly allowed files
172174
allow: [
173175
path.resolve(__dirname),
174176
path.resolve(__dirname, 'node_modules')
175-
],
176-
strict: false
177+
]
177178
}
178179
}
179180
})

0 commit comments

Comments
 (0)