Skip to content

Commit

Permalink
feat(after): add loadStaticProps
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaa77 committed Jul 11, 2020
1 parent 2c79809 commit d78b967
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/after.js/src/loadStaticProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AsyncRouteProps, InitialData, CtxBase } from './types';

const PAGE_DATA_FILE_NAME = 'page-data.json';

// TODO: for better performance we can check
// and see if matchedRoute component have getInitialProps or not
// if the page dose not have a getInitalProps, then page-data.json is empty

/**
* reads data from pathname/page-data.json file using (fetch method) and then return it
* @param routes
* @param pathname
*/
export async function loadStaticProps(
_routes: AsyncRouteProps[],
pathname: string,
_ctx: CtxBase
): Promise<{ data: InitialData }> {
// in ssg mode there is always a file called page-data.json next to the
// page path that we have in our app this page-data.json file includes inital data that we need
// to inject to our components before we render the component
const PAGE_DATA_FILE_PATH = `${pathname}/${PAGE_DATA_FILE_NAME}`;
return fetch(PAGE_DATA_FILE_PATH)
.then(res => res.json())
.then(res => {
return { data: res.data };
});
}

0 comments on commit d78b967

Please sign in to comment.