Comparing version 0.0.13 to 0.0.14
@@ -10,3 +10,3 @@ # Changelog | ||
## [v0.0.13](https://github.com/sw-yx/ssg/compare/v0.0.3...v0.0.13) - 2019-09-14 | ||
## [v0.0.14](https://github.com/sw-yx/ssg/compare/v0.0.3...v0.0.14) - 2019-09-14 | ||
@@ -16,4 +16,4 @@ ### Commits | ||
- swap implementation to fork sapper cli [`c903325`](https://github.com/sw-yx/ssg/commit/c903325cf370772645c10283b40cb2d29f75ff37) | ||
- docs [`c504ceb`](https://github.com/sw-yx/ssg/commit/c504ceb9117fcd9d98bf581907ce7f24d1922e52) | ||
- add examples and source-yaml [`93d1f95`](https://github.com/sw-yx/ssg/commit/93d1f953ec72308d4cef15da15703e61c78a4632) | ||
- restart server [`dc93c50`](https://github.com/sw-yx/ssg/commit/dc93c50edae44054c1008538609bcffcc2c27c88) | ||
@@ -20,0 +20,0 @@ ## [v0.0.3](https://github.com/sw-yx/ssg/compare/v0.0.2...v0.0.3) - 2019-09-12 |
@@ -56,3 +56,2 @@ "use strict"; | ||
const { dev } = yield Promise.resolve().then(() => __importStar(require('sapper/api'))); | ||
const { getSSGDataOnce, watchSSGFiles, readSSGConfig } = yield Promise.resolve().then(() => __importStar(require('./cli-ssg'))); | ||
try { | ||
@@ -81,3 +80,3 @@ const watcher = dev({ | ||
*/ | ||
console.log('SSGDEBUG ssgconfig', opts.ssgConfig); | ||
const { getSSGDataOnce, watchSSGFiles, readSSGConfig } = yield Promise.resolve().then(() => __importStar(require('./cli-ssg'))); | ||
let ssgConfigPath = opts.ssgConfig || 'ssg.config.js'; | ||
@@ -237,2 +236,20 @@ const ssgConfig = readSSGConfig(ssgConfigPath); | ||
const { default: pb } = yield Promise.resolve().then(() => __importStar(require('pretty-bytes'))); | ||
/** | ||
* | ||
* SSG SECTION | ||
* | ||
* verify ssg config exists | ||
* | ||
*/ | ||
const { getSSGDataOnce, readSSGConfig } = yield Promise.resolve().then(() => __importStar(require('./cli-ssg'))); | ||
let ssgConfigPath = opts.ssgConfig || 'ssg.config.js'; | ||
const ssgConfig = readSSGConfig(ssgConfigPath); | ||
// actually do stuff with it | ||
yield getSSGDataOnce(ssgConfig); | ||
/** | ||
* | ||
* END SSG SECTION | ||
* | ||
* | ||
*/ | ||
yield _export({ | ||
@@ -239,0 +256,0 @@ cwd: opts.cwd, |
{ | ||
"name": "ssg", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "bin": { |
@@ -22,6 +22,9 @@ # Sapper Site Generator | ||
const { slug } = req.params | ||
const data = await getData() | ||
if (data[slug]) { | ||
const splitSlug = slug.split('___ssg___') | ||
const category = splitSlug[0] | ||
const realSlug = splitSlug[1] | ||
const data = await getData(category, realSlug) | ||
if (data) { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }) | ||
res.end(JSON.stringify(data[slug])) | ||
res.end(JSON.stringify(data)) | ||
} else { | ||
@@ -34,7 +37,16 @@ res.writeHead(404, { 'Content-Type': 'application/json' }) | ||
2. You should have a `ssg.config.js` that exports a `getData` function that provides this data: | ||
2. You should have a `ssg.config.js` that exports a `getInitialData` (run once) and `getData` (run each time) function that provides this data: | ||
```js | ||
exports.getData = async () => { | ||
// do whatever you want | ||
exports.getData = async (category, slug) => { | ||
// read cache and | ||
// do less expensive subsequent fetches | ||
const data = require(path.resolve('.ssg/data.json')) | ||
const result = data[category][slug] | ||
if (typeof result === 'undefined') throw new Error('no data found for ' + slug) | ||
return result | ||
} | ||
exports.getInitialData = async () => { | ||
// do expensive initial fetches and cache them in .ssg/data.json | ||
return { | ||
@@ -48,4 +60,24 @@ index: [{ title: 'foo', slug: 'foo' }, { title: 'bar', slug: 'bar' }], | ||
In your templates, you may now query this data at any time: | ||
```html | ||
<!-- src/routes/talks/[slug].svelte --> | ||
<script context="module"> | ||
export async function preload({ params, query }) { | ||
const res = await this.fetch(`data/talks___ssg___${params.slug}.json`) | ||
const data = await res.json() | ||
if (res.status === 200) { | ||
return { post: data } | ||
} else { | ||
this.error(res.status, data.message) | ||
} | ||
} | ||
</script> | ||
``` | ||
## What it does | ||
Under the hood, `ssg` runs `sapper dev` for you, and watches and reloads it whenever you change your config or contents folder. | ||
It runs `getInitialData` once and saves that result to a cache, and then you can run `getData` anytime you want to query that cache. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27926
568
80