-
Notifications
You must be signed in to change notification settings - Fork 1
/
next.config.js
77 lines (66 loc) · 1.85 KB
/
next.config.js
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
const fs = require('fs')
const yaml = require('js-yaml')
const axios = require('axios')
const Pbf = require('pbf')
const geobuf = require('geobuf')
let publicRuntimeConfig = {}
if (fs.existsSync('./data/config.yml')) {
publicRuntimeConfig = yaml.load(fs.readFileSync('./data/config.yml', 'utf8'))
}
const fetchPBF = async (url) => {
return await axios.get(url, {
responseType: 'arraybuffer',
})
}
const downloadPbf = async () => {
let existsPBF = false
try {
existsPBF = fs.existsSync('./public/geonature.pbf')
} catch (e) {}
if (publicRuntimeConfig?.dependencies?.pbf && !existsPBF) {
const { data, status } = await fetchPBF(
publicRuntimeConfig?.dependencies?.pbf
)
if (status === 200) {
fs.writeFileSync('./public/geonature.pbf', data)
}
}
}
module.exports = {
publicRuntimeConfig,
trailingSlash: true,
exportPathMap: async function () {
await downloadPbf()
const pbfData = fs.readFileSync('./public/geonature.pbf')
const pbf = new Pbf(pbfData)
const geojson = geobuf.decode(pbf)
// Treating geojson features since arrays are not well formatted
// It seems to come from PostGis St_AsGeoBuf...
geojson.features = geojson.features.map((f) => {
const { json_arrays, ...properties } = f.properties
return {
...f,
properties: {
...properties,
...JSON.parse(json_arrays || '{}'),
},
}
})
const buffer = geobuf.encode(geojson, new Pbf())
fs.writeFileSync('./public/geonature.pbf', buffer)
const routes = geojson.features.reduce((acc, value) => {
acc[`/map/${value.properties.slug}`] = {
page: '/map/[slug]',
query: {
slug: value.properties.slug,
},
}
return acc
}, {})
return {
'/': { page: '/' },
'/map': { page: '/map' },
...routes,
}
},
}