Skip to content

Commit ca216ec

Browse files
committed
Sync _redirects only for production build
1 parent 5d7fd75 commit ca216ec

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public/blog/feed.xml
77

88
node_modules/
99
.next/
10+
out/
1011
index_data/*.json
1112

1213
# Generated via test examples script

next.config.mjs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,28 @@ const config = {
154154
},
155155
];
156156

157-
const redirectsFile = path.join(import.meta.dirname, "public/_redirects");
158-
await fs.writeFile(
159-
redirectsFile,
160-
redirects
161-
.map(({ source, destination, permanent }) => {
162-
return `${source} ${destination} ${permanent ? 308 : 307}`;
163-
})
164-
.join("\n") +
165-
"\n" +
166-
splatRedirects
167-
.map(({ source, destination, permanent }) => {
168-
const splatPattern = /:(\w+)\*$/;
169-
assert.match(source, splatPattern);
170-
assert.match(destination, splatPattern);
171-
return `${source.replace(splatPattern, "*")} ${destination.replace(splatPattern, ":splat")} ${permanent ? 308 : 307}`;
172-
})
173-
.join("\n"),
174-
"utf8",
175-
);
157+
if (process.env.NODE_ENV === "production") {
158+
const redirectsFile = path.join(import.meta.dirname, "out/_redirects");
159+
await fs.mkdir(path.dirname(redirectsFile));
160+
await fs.writeFile(
161+
redirectsFile,
162+
redirects
163+
.map(({ source, destination, permanent }) => {
164+
return `${source} ${destination} ${permanent ? 308 : 307}`;
165+
})
166+
.join("\n") +
167+
"\n" +
168+
splatRedirects
169+
.map(({ source, destination, permanent }) => {
170+
const splatPattern = /:(\w+)\*$/;
171+
assert.match(source, splatPattern);
172+
assert.match(destination, splatPattern);
173+
return `${source.replace(splatPattern, "*")} ${destination.replace(splatPattern, ":splat")} ${permanent ? 308 : 307}`;
174+
})
175+
.join("\n"),
176+
"utf8",
177+
);
178+
}
176179

177180
return [...redirects, ...splatRedirects];
178181
},

public/_redirects

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)