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

Commit

Permalink
Merge branch 'master' into feature/images
Browse files Browse the repository at this point in the history
  • Loading branch information
birkir authored Jul 9, 2019
2 parents f598dcd + f79a0b3 commit 13482f5
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start": "lerna run start --ignore \"@examples/*\" --stream",
"build": "lerna run build --ignore \"@examples/*\"",
"prettier": "prettier --write '**/*.{json,md,js,jsx,ts,tsx}'",
"test": "lerna run build --scope \"@examples/*\"",
"test": "lerna run build --scope \"@examples/*\" --parallel",
"test:prettier": "prettier --list-different '**/*.{json,md,js,jsx,ts,tsx}'"
},
"husky": {
Expand Down
131 changes: 78 additions & 53 deletions packages/gatsby-source-prismic-graphql/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,44 @@ exports.sourceNodes = (ref: any, options: PluginOptions) => {
return sourceNodes(ref, opts);
};

const getPagesQuery = ({ pageType }: { pageType: string }) => `
query AllPagesQuery (
$after: String
) {
prismic {
${pageType} (
first: 20
after: $after
) {
totalCount
pageInfo {
hasNextPage
endCursor
}
edges {
node {
_meta {
id
lang
uid
alternateLanguages {
id
lang
type
uid
}
}
}
}
}
}
}
`;

exports.createPages = async ({ graphql, actions: { createPage } }: any, options: PluginOptions) => {
const previewPath = options.previewPath || '/preview';

// Create top-level preview page
createPage({
path: previewPath.replace(/^\//, ''),
component: path.resolve(path.join(__dirname, 'components', 'PreviewPage.js')),
Expand All @@ -45,64 +80,49 @@ exports.createPages = async ({ graphql, actions: { createPage } }: any, options:
},
});

await Promise.all(
(options.pages || []).map(async page => {
const queryKey = `all${page.type}s`;
const query = `
query {
prismic {
${queryKey} {
edges {
node {
_meta {
id
lang
uid
alternateLanguages {
id
lang
type
uid
}
}
}
}
}
}
}
`;
// Helper that recursively creates 20 pages at a time for the given page type
// (Prismic GraphQL queries only return up to 20 results per query)
async function createPageRecursively(page: any, endCursor: string = '') {
const pageType = `all${page.type}s`;
const query = getPagesQuery({ pageType });
const { data, errors } = await graphql(query, { after: endCursor });
const toPath = pathToRegexp.compile(page.match || page.path);
const rootQuery = getRootQuery(page.component);

if (errors && errors.length) {
throw errors[0];
}

const { data, errors } = await graphql(query);
const toPath = pathToRegexp.compile(page.match || page.path);
const rootQuery = getRootQuery(page.component);
const hasNextPage = data.prismic[pageType].pageInfo.hasNextPage;
endCursor = data.prismic[pageType].pageInfo.endCursor;

if (errors && errors.length) {
throw errors[0];
// Cycle through each page returned from query...
data.prismic[pageType].edges.forEach(({ node }: any) => {
const params = {
...node._meta,
lang: node._meta.lang === options.defaultLang ? null : node._meta.lang,
};
const path = toPath(params);

if (page.lang && page.lang !== node._meta.lang) {
return; // don't generate page in other than set language
}

data.prismic[queryKey].edges.forEach(({ node }: any) => {
const params = {
// ...and create the page
createPage({
path: path === '' ? '/' : path,
component: page.component,
context: {
rootQuery,
...node._meta,
lang: node._meta.lang === options.defaultLang ? null : node._meta.lang,
};
const path = toPath(params);

if (page.lang && page.lang !== node._meta.lang) {
// don't generate page in other than set language.
return;
}

createPage({
path: path === '' ? '/' : path,
component: page.component,
context: {
rootQuery,
...node._meta,
},
});
},
});
});

// used for preview placeholder page
if (hasNextPage) {
await createPageRecursively(page, endCursor);
} else {
// If there are no more pages, create the preview page for this page type
createPage({
path: page.path,
matchPath: process.env.NODE_ENV === 'production' ? undefined : page.match,
Expand All @@ -114,8 +134,13 @@ exports.createPages = async ({ graphql, actions: { createPage } }: any, options:
lang: options.defaultLang,
},
});
})
);
}
}

// Create all the pages!
const pages = options.pages || [];
const pageCreators = pages.map(page => createPageRecursively(page));
await Promise.all(pageCreators);
};

exports.createResolvers = (
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby-source-prismic-graphql/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export function fetchStripQueryWhitespace(url: string, ...args: any) {
const [hostname, qs = ''] = url.split('?');
const queryString = parseQueryString(qs);
if (queryString.has('query')) {
queryString.set('query', String(queryString.get('query')).replace(/\#.*\n/g, '').replace(/\s+/g, ' '));
queryString.set(
'query',
String(queryString.get('query'))
.replace(/\#.*\n/g, '')
.replace(/\s+/g, ' ')
);
}
const updatedQs = Array.from(queryString)
.map(n => n.map(j => encodeURIComponent(j)).join('='))
Expand Down Expand Up @@ -68,7 +73,8 @@ export function PrismicLink({ uri, accessToken, customRef, ...rest }: IPrismicLi
const authorizationHeader = accessToken ? { Authorization: `Token ${accessToken}` } : {};

// if custom ref has been defined, then use that to pull content instead of default master ref
prismicRef = (typeof customRef === 'undefined' || customRef === null) ? prismicRef : customRef;
prismicRef =
typeof customRef === 'undefined' || customRef === null ? prismicRef : customRef;

return {
headers: {
Expand Down

0 comments on commit 13482f5

Please sign in to comment.