Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions packages/gatsby-source-strapi/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const fetchEntity = async (
) => {
const { reporter, axiosInstance } = context;

const locale = pluginOptions?.i18n?.locale;
const otherLocales = [];

if (locale && locale === "all") {
delete queryParams.locale;
}

/** @type AxiosRequestConfig */
const options = {
method: "GET",
Expand All @@ -63,32 +70,23 @@ export const fetchEntity = async (
} with ${options.paramsSerializer.serialize(options.params)}`,
);

// Handle internationalization
const locale = pluginOptions?.i18n?.locale;
const otherLocales = [];

if (locale) {
// Ignore queryParams locale in favor of pluginOptions
delete queryParams.locale;

if (locale === "all") {
// Get all available locales
const { data: response } = await axiosInstance({
...options,
params: {
populate: {
localizations: {
fields: ["locale"],
},
if (locale && locale === 'all') {
// Get all available locales
const { data: response } = await axiosInstance({
...options,
params: {
populate: {
localizations: {
fields: ["locale"],
},
},
});
for (const localization of response.data.attributes.localizations.data) {
otherLocales.push(localization.attributes.locale);
}
} else {
// Only one locale
queryParams.locale = locale;
},
});
options.params.locale = response.data.attributes?.locale || response.data.locale;
// Strapi v5 support
const localizations = response.data.attributes?.localizations.data || response.data.localizations;
for (const localization of localizations) {
otherLocales.push(localization.attributes?.locale || localization.locale);
}
}

Expand Down Expand Up @@ -130,6 +128,15 @@ export const fetchEntities = async (
) => {
const { reporter, axiosInstance } = context;

// Use locale from pluginOptions if it's defined
if (pluginOptions?.i18n?.locale) {
delete queryParams.locale;
let locale = pluginOptions.i18n.locale
if (version === 5 && pluginOptions.i18n.locale === 'all')
locale = '*';
queryParams.locale = locale;
}

/** @type AxiosRequestConfig */
const options = {
method: "GET",
Expand All @@ -140,12 +147,6 @@ export const fetchEntities = async (
},
};

// Use locale from pluginOptions if it's defined
if (pluginOptions?.i18n?.locale) {
delete queryParams.locale;
queryParams.locale = pluginOptions.i18n.locale;
}

try {
reporter.info(
`Starting to fetch data from Strapi - ${
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-source-strapi/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ export const createNodes = (entity, context, uid) => {
// also, support both v5 documentId and v4 id
const strapi_document_id_or_regular_id = entity.documentId || entity.id;

// localizations of the same entity will have the same documentId
// so the locale is included to prevent localized entries from overwriting each other
const locale = entity.locale ? `-${entity.locale}` : '';

let entryNode = {
id: createNodeId(`${nodeType}-${strapi_document_id_or_regular_id}`),
id: createNodeId(`${nodeType}-${strapi_document_id_or_regular_id}${locale}`),
documentId: entity.documentId,
strapi_id: entity.id,
strapi_document_id_or_regular_id,
Expand Down