Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSG: support wildcards in static paths #666

Merged
merged 15 commits into from
May 5, 2024
Merged

Conversation

pmelab
Copy link
Contributor

@pmelab pmelab commented Apr 23, 2024

Allow wildcards in static paths. The main use case would be scenarios where waku is used as a static site generator and paths are controlled by a headless CMS.

Copy link

vercel bot commented Apr 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview May 5, 2024 9:55am

Copy link

codesandbox-ci bot commented Apr 23, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@dai-shi
Copy link
Owner

dai-shi commented Apr 23, 2024

Wow, thanks for working on it. Let me do a quick first review.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


let standaloneDir: string;
const exampleDir = fileURLToPath(
new URL('../examples/25_static-wildcard', import.meta.url),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move it to fixtures instead of examples?

// Which will cause files in `src` folder to be empty.
// I don't know why
const tmpDir = process.env.TEMP_DIR ? process.env.TEMP_DIR : tmpdir();
standaloneDir = await mkdtemp(join(tmpDir, 'waku-07-router-'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

07-router sounds strange here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy & paste 🙈

break;
case 'group':
pathItems.push(staticPath[slugIndex++]!);
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, now you read the create-page.ts code, do you have any idea to improve it or completely rewrite it?
I wonder how readable the code is. It's unlikely that the code is easy to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did find my way in reasonable time, so it can't be that bad 😄
I fully agree with the comment on line 29, but there is no unit test framework. I suppose we would use vitest?

// FIXME we should add unit tests for some functions and type utils.

Happy to take a stab at this, in a separate PR.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be more than helpful!
Yes, let's add vitest. A separate PR would be perfect.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, ts-expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There we go: #667

I created a pure setup-PR first, since i'm also working on another PR where vitest would help.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you add some tests for this?
Maybe we should have a separate PR to add tests without this PR change first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not too easy to find a way to test it. But i think this gives good coverage to do a confident refactor in a later step: #692

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the unit tests immediately revealed a regression! 😅

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice! Yeah, because I was very unsure that I could review the code.

@pmelab pmelab requested a review from dai-shi April 23, 2024 09:06
This was referenced Apr 23, 2024
Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first review

thanks for working on it. this seems like a good improvement.

Comment on lines +22 to +25
// GitHub Action on Windows doesn't support mkdtemp on global temp dir,
// Which will cause files in `src` folder to be empty.
// I don't know why
const tmpDir = process.env.TEMP_DIR ? process.env.TEMP_DIR : tmpdir();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, does that mean TEMP_DIR is a workaround for Windows?

@himself65 thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but I think we could just make things under .cache folder? I forget why I prefer to use tmpdir here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this comment is copied from e2e/07_router_standalone.spec.ts.
Okay, please tackle it separately from this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, copy from my code 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, copy from my code 😆

I like to call it "proudly found elsewhere".

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
break;
case 'group':
pathItems.push(staticPath[slugIndex++]!);
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you add some tests for this?
Maybe we should have a separate PR to add tests without this PR change first.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

second review

Comment on lines +7 to +12
export const getConfig = async () => {
return {
render: 'static',
staticPaths: [[], 'foo', ['bar', 'baz']],
};
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't type checked.
I wonder whether we should make ssg-wildcard with createPages API instead of fs-router.

We can say that types are covered with unit tests, but still fixtures should be as basic as possible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it is not type-covered in createPages either:

// TODO: This fails at runtime, but not at type level.
// \@ts-expect-error: static paths do not match the slug pattern
createPage({
render: 'static',
path: '/test/[a]/[b]',
staticPaths: ['c'],
component: () => 'Hello',
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see. I hope we can fix it.

My question still holds: Should we make fixtures use low-level createPages instead of fs-router? (Unless we specifically tests fs-router.)
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. My gut feeling would be to use integration tests to cover as much ground as possible, but in this case createPages is a common entry point (and even my preferred one). Also fs-router.ts is a good target for a unit test.
But this then should also apply to partial-build and ssg-performance. And most other tests don't use createPages but defineRouter. Maybe another PR where we look at the e2e setup holistically, including the typing hiccups that we encountered?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Sounds all good!

break;
case 'group':
pathItems.push(staticPath[slugIndex++]!);
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice! Yeah, because I was very unsure that I could review the code.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't fully read the code carefully, but let's trust the tests.

@dai-shi dai-shi merged commit 0f7204b into dai-shi:main May 5, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants