[question] Using objects as template placeholders
See original GitHub issueHi,
I was trying out preval with NextJS to inline my data during build time.
This is my code:
static async getInitialProps() {
const { data } = await Axios.get(
"http://localhost:3100/graphql?query={ articles{ name slug } }"
);
// data.data is an object
const actualData = preval`
const allArticles = ${data.data}
module.exports = allArticles
`;
console.log(actualData);
return { data };
}
When I console log it, it returns an empty object. Am I doing something wrong here?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Chapter 19. Templates: filling placeholders with data
This chapter shows you how to untangle the HTML from the data and the JavaScript by using templates. The designers on your team...
Read more >C++ making a template class as place holder in another class
For this you first need a way to represet a C++ type (not object) into an ... And post another question better suited...
Read more >Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick ... The strings and placeholders get passed to a function — either a default function ...
Read more >Template Method Design Pattern - SourceMaking
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Base class declares algorithm 'placeholders', ...
Read more >Problem adding objects to powerpoint slides when using ...
Hi, I'm having this problem. I'm working on a template to build automatic reports. The idea is to read data from an excel...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, that’s tricky. What would probably be best is to have a script that loads the data into a cache before the build so you can get it synchronously during the build.
But if that doesn’t work, your code can do
spawn.sync
to run an asynchronous script synchronously. But that would make your build pretty slow.Hi @kentcdodds .
The use case @rtm619 is trying to achieve is to inline the data when doing a next.js export, that way when the site is served from the exported files that the getInitialProps function is returning the inlined content instead of going to the API. I think the problem is that preval only supports sync and the call here is async. Any ideas how to potentially do this using preval?