Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Commit

Permalink
feat(config):
Browse files Browse the repository at this point in the history
- add possibility to declare nuxtent options `api` key as a function
  given the `isStatic` parameter to adjust Axios options when using `nuxt generate`

```json
{
  content: {
    page: '/_slug',
    permalink: '/:year/:slug',
    generate: ['get', 'getAll']
  },
  api: function(isStatic) {
    return {
      browserBaseURL: !isStatic ? '' : 'https://my.company.com'
    }
  }
}
```
- all nuxtent options present in `api` key are now forwarded to axios

fixes #92
  • Loading branch information
medfreeman committed Sep 29, 2017
1 parent d519f29 commit 6beb187
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 53 deletions.
16 changes: 12 additions & 4 deletions lib/content/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const response = res => ({
}
})

const curryResponseHandler = (api, endpoint, contentDir, dirOpts, isDev) => {
const curryResponseHandler = (
baseUrl,
serverPrefix,
endpoint,
contentDir,
dirOpts,
isDev
) => {
const db = createDatabase(contentDir, endpoint, dirOpts, isDev)

return function sendContent(req, res) {
Expand All @@ -39,7 +46,7 @@ const curryResponseHandler = (api, endpoint, contentDir, dirOpts, isDev) => {
const [_, queryStr] = req.url.match(/\?(.*)/) || []
const { only, between, ...query } = parse(queryStr)

logRequest(api.serverPrefix, api.baseURL + permalink)
logRequest(serverPrefix, baseUrl + permalink)

if (permalink === '/') {
// request multiple pages from directory
Expand All @@ -61,7 +68,7 @@ const curryResponseHandler = (api, endpoint, contentDir, dirOpts, isDev) => {
}
}

const createRouter = (api, options) => {
const createRouter = (baseUrl, serverPrefix, options) => {
const router = Router()
const { contentDir, content, parsers, isDev } = options

Expand All @@ -81,7 +88,8 @@ const createRouter = (api, options) => {
const dirOpts = { ...content[dirName], parsers }

const sendContent = curryResponseHandler(
api,
baseUrl,
serverPrefix,
dirName,
contentDir,
dirOpts,
Expand Down
122 changes: 73 additions & 49 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,31 @@ const mergeContentOptions = (content, defaults) => {
return opts
}

const getAPIOptions = (originalOptions = {}, isStatic) => {
console.log('typeof originalOptions: ', typeof originalOptions)
const options =
typeof originalOptions === 'function'
? originalOptions(isStatic)
: originalOptions

const {
baseURL = '',
browserBaseURL = undefined,
otherAPIOptions = {}
} = options

return {
baseURL,
browserBaseURL: browserBaseURL || baseURL,
...otherAPIOptions
}
}

const CONTENT_DIR = 'content'
const COMPONENTS_DIR = 'components'
const BUILD_DIR = 'content'
const API_SERVER_PREFIX = '/content-api'
const API_BROWSER_PREFIX = '/_nuxt/content'

export default function ContentModule(moduleOpts) {
const userOptions =
Expand All @@ -68,13 +90,6 @@ export default function ContentModule(moduleOpts) {
const isDev = this.nuxt.options.dev
const loaderComponentExtensions = ['.vue', '.js']

const api = {
baseURL: '',
...userOptions.api,
serverPrefix: `/content-api`,
browserPrefix: `/_nuxt/content`
}

const parsers = {
md: Object.assign(
{},
Expand All @@ -97,7 +112,56 @@ export default function ContentModule(moduleOpts) {
isDev
}

// 1. Generate Vue templates from markdown with components (*.comp.md)
// Add `$content` helper
this.addPlugin({
src: resolve(__dirname, 'plugins/requestContent.js')
})

// Add content API when running `nuxt` & `nuxt build` (development and production)
this.addServerMiddleware({
path: API_SERVER_PREFIX,
handler: createRouter(
getAPIOptions(userOptions.api, false).baseURL,
API_SERVER_PREFIX,
routesOptions
)
})

this.nuxt.plugin('build', builder => {
const isStatic = builder.isStatic
const apiOptions = getAPIOptions(userOptions.api, isStatic)

// Add content API when running `nuxt generate`
this.nuxt.plugin('generator', generator => {
const app = express()
app.use(
API_SERVER_PREFIX,
createRouter(apiOptions.baseURL, API_SERVER_PREFIX, routesOptions)
)
const server = app.listen(port)

generator.plugin('generated', () => {
server.close()
})
})

// Initialize axios module
this.requireModule([
'@nuxtjs/axios',
{
...apiOptions,
baseURL: apiOptions.baseURL + API_SERVER_PREFIX,
browserBaseURL:
apiOptions.browserBaseURL +
(!isStatic ? API_SERVER_PREFIX : API_BROWSER_PREFIX)
}
])

// Build dynamic content pages without components (*.md)
buildContent(this, BUILD_DIR, isStatic, routesOptions)
})

// Generate Vue templates from markdown with components (*.comp.md)
this.extendBuild(config => {
config.module.rules.push({
test: /\.comp\.md$/,
Expand All @@ -116,47 +180,7 @@ export default function ContentModule(moduleOpts) {
})
})

this.nuxt.plugin('build', builder => {
// 1. Initialize axios module
this.requireModule([
'@nuxtjs/axios',
{
baseURL: api.baseURL + api.serverPrefix,
browserBaseURL:
api.baseURL +
(builder.isStatic ? api.browserPrefix : api.serverPrefix)
}
])

// 2. Build dynamic content pages without components (*.md)
buildContent(this, BUILD_DIR, builder.isStatic, routesOptions)
})

this.addPlugin({
src: resolve(__dirname, 'plugins/requestContent.js')
})

// 3. Add content API
const router = createRouter(api, routesOptions)

// Add API when running `nuxt` & `nuxt build` (development and production)
this.addServerMiddleware({
path: api.serverPrefix,
handler: router
})

// Add API when running `nuxt generate`
this.nuxt.plugin('generator', generator => {
const app = express()
app.use(api.serverPrefix, router)
const server = app.listen(port)

generator.plugin('generated', () => {
server.close()
})
})

// 4. Add Vue templates generated from markdown to output build
// Add Vue templates generated from markdown with components (*.comp.md) to output build
this.addPlugin({
src: resolve(__dirname, 'plugins/markdownComponents.template.js'),
options: {
Expand Down

0 comments on commit 6beb187

Please sign in to comment.