You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
szkp-map-web/vite.tianditu-proxy.ts

31 lines
1.0 KiB

import type { ProxyOptions } from 'vite'
/** 本地 dev 代理天地图时伪造的 Referer须已在天地图控制台白名单中勿填 localhost */
const DEV_REFERER = process.env.VITE_TIANDITU_DEV_REFERER?.trim() || 'https://szkp-map.langye.net/'
const refererOnProxy: ProxyOptions['configure'] = (proxy) => {
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Referer', DEV_REFERER)
})
}
/** 供 vite.config 使用localhost 无法入白名单时,走同源代理加载 SDK 与瓦片 */
export function tiandituDevProxy(): Record<string, ProxyOptions> {
return {
'/tianditu-tile-proxy': {
target: 'https://t0.tianditu.gov.cn',
changeOrigin: true,
secure: true,
rewrite: (path) => path.replace(/^\/tianditu-tile-proxy/, ''),
configure: refererOnProxy,
},
'/tianditu-api-proxy': {
target: 'https://api.tianditu.gov.cn',
changeOrigin: true,
secure: true,
rewrite: (path) => path.replace(/^\/tianditu-api-proxy/, ''),
configure: refererOnProxy,
},
}
}