如何在 next.js getInitialProps 中调用 Prismic API?
How do I call a Prismic API inside next.js getInitialProps?
如何调用Prismic (CMS) API, from a next.js application?在 next.js 我之前有过:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
const res = await fetch('https://myAPI_URL')
const json = await res.json()
return { cars: json.results }
}
render () {
return (
<div>
<p>I can see the data {this.props.cars} </p>
</div>
)
}
}
但我想使用 Prismic 开发工具包作为依赖项,如所述in their documentation.
npm install prismic-javascript prismic-dom --save
以及调用 Prismic 的代码:
const Prismic = require('prismic-javascript');
const apiEndpoint = "https://your-repository-name.prismic.io/api/v2";
const apiToken = "this_is_a_fake_token_M1NrQUFDWUE0RYvQnvv70";
Prismic.getApi(apiEndpoint, {accessToken: apiToken}).then(function(api) {
return api.query(""); // An empty query will return all the documents
}).then(function(response) {
console.log("Documents: ", response.results);
}, function(err) {
console.log("Something went wrong: ", err);
});
但是如何在 getInitialProps 中使 Prismic API 调用与 next.js 一起工作?例如:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
// fetch Prismic API and return the results
}
render () {
return (
<div>
<p>Showing data fetched from API here</p>
</div>
)
}
}
您已经准备好所有组件。等待 Prismic
请求,而不是 fetch
。然后return结果。
import React from 'react';
import Prismic from 'prismic-javascript';
const apiEndpoint = 'https://project.prismic.io/api/v2';
const apiToken = 'TOKEN';
export default class Index extends React.Component {
static async getInitialProps({ req, query }) {
const data = await Prismic.getApi(apiEndpoint, { accessToken: apiToken })
.then(api => {
return api.query(''); // An empty query will return all the documents
})
.catch(err => console.log(err));
return { cars: data.results };
}
render() {
return <div>Example car id: {this.props.cars[0].id}</div>;
}
}
如何调用Prismic (CMS) API, from a next.js application?在 next.js 我之前有过:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
const res = await fetch('https://myAPI_URL')
const json = await res.json()
return { cars: json.results }
}
render () {
return (
<div>
<p>I can see the data {this.props.cars} </p>
</div>
)
}
}
但我想使用 Prismic 开发工具包作为依赖项,如所述in their documentation.
npm install prismic-javascript prismic-dom --save
以及调用 Prismic 的代码:
const Prismic = require('prismic-javascript');
const apiEndpoint = "https://your-repository-name.prismic.io/api/v2";
const apiToken = "this_is_a_fake_token_M1NrQUFDWUE0RYvQnvv70";
Prismic.getApi(apiEndpoint, {accessToken: apiToken}).then(function(api) {
return api.query(""); // An empty query will return all the documents
}).then(function(response) {
console.log("Documents: ", response.results);
}, function(err) {
console.log("Something went wrong: ", err);
});
但是如何在 getInitialProps 中使 Prismic API 调用与 next.js 一起工作?例如:
import React from 'react'
import Link from 'next/link'
import 'isomorphic-fetch'
export default class Index extends React.Component {
static async getInitialProps () {
// fetch Prismic API and return the results
}
render () {
return (
<div>
<p>Showing data fetched from API here</p>
</div>
)
}
}
您已经准备好所有组件。等待 Prismic
请求,而不是 fetch
。然后return结果。
import React from 'react';
import Prismic from 'prismic-javascript';
const apiEndpoint = 'https://project.prismic.io/api/v2';
const apiToken = 'TOKEN';
export default class Index extends React.Component {
static async getInitialProps({ req, query }) {
const data = await Prismic.getApi(apiEndpoint, { accessToken: apiToken })
.then(api => {
return api.query(''); // An empty query will return all the documents
})
.catch(err => console.log(err));
return { cars: data.results };
}
render() {
return <div>Example car id: {this.props.cars[0].id}</div>;
}
}