生产环境下页面刷新后Tailwind类无效。

5

问题陈述:

我有一个使用nextjstailwindcss的项目。在登录页面上,UI在第一次加载时有必要的类,但如果我刷新页面,则这些类会从DOM中消失,导致UI出现问题。

这是该站点登录页面的部署链接

如何重现?

  1. 打开上面给定的链接,您将观察到登录表单UI看起来还好

enter image description here

  1. Ctrl+R(刷新页面),您将观察到登录界面现在已经损坏

enter image description here

代码文件

tailwind.config.js

const colors = require('tailwindcss/colors')
const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
    purge: {
        content:[
        './src/pages/**/*.js',
        './src/pages/**/*.ts',
        './src/pages/**/*.tsx',
        './src/design-system/**/*.js',
        './src/design-system/**/*.ts',
        './src/design-system/**/*.tsx',
        './src/components/**/*.js',
        './src/components/**/*.ts',
        './src/components/**/*.tsx'
    ],
    
    // options: {whitelist:['h-52', 'py-9', 'max-w-2xl', 'text-white', 'h-screen']}
},
    darkMode: false, // or 'media' or 'class'
    theme: {
        fontSize: {
            'xxs': '10px',
            'xs': '.75rem',
            'sm': '.875rem',
            'tiny': '.875rem',
            'base': '1rem',
            'lg': '1.125rem',
            'xl': '1.25rem',
            '2xl': '1.5rem',
            '3xl': '1.875rem',
            '4xl': '2.25rem',
            '5xl': '3rem',
            '6xl': '4rem',
            '7xl': '5rem'
        },
        flex: {
            1: '1 1 0%',
            '30p': '0 0 30%',
            auto: '1 1 auto',
            initial: '0 1 auto',
            inherit: 'inherit',
            none: 'none',
            2: '2 2 0%',
            full: '0 0 100%',
            half: '0 0 50%'
        },
        colors: {
            white: colors.white,
            gray: colors.trueGray,
            indigo: colors.indigo,
            green: colors.green,
            red: colors.rose,
            rose: colors.rose,
            purple: colors.purple,
            orange: colors.orange,
            'light-blue': colors.lightBlue,
            fuchsia: colors.fuchsia,
            pink: colors.pink,
            cyan: colors.cyan,

            // NEW UI COLORS
            'CD-blue': '#2357DE',
            'CD-blue-accent': '#4770FF',
            'CD-black-dark': '#1D1D1D',
            'CD-black-dark-accent': '#202020',
            'CD-black-medium-dark': '#242424',
            'CD-black-extra-dark': '#1B1B1B',
            'CD-black-light': '#2E2E2E',
            'CD-gray': '#3E3E3E',
            'CD-gray-accent': '#353535',
            'CD-red-accent': '#FF745F',
            'CD-yellow-accent': '#FFC167'
        },
        minHeight: {
            0: '0',
            '1/4': '25%',
            '1/2': '50%',
            '3/4': '75%',
            full: '100%',
            '90vh': '90vh'
        },
        minWidth: {
            0: '0',
            '1/4': '25%',
            '1/2': '50%',
            '3/4': '75%',
            full: '100%',
            '250px': '250px'
        },
        screens: {
            xs: { min: '0px', max: '390px' },
            ...defaultTheme.screens
        },
        extend: {}
    },
    variants: {
        extend: {}
    },
    plugins: []
}

login.jsx --> 登录界面的JSX

<div>
<div className="h-screen w-full flex justify-center items-center mx-auto max-w-2xl text-white">
                <div className="w-full md:min-w-full bg-CD-black-dark-accent rounded px-8 mx-4 sm:px-16 py-10">
                    <div className="text-center mb-16">
                        <h1 className="text-3xl">Creator Login</h1>
                    </div>
                    <div className="space-y-4">
                        <Input
                            label="Enter username"
                            type="text"
                            placeholder="For e.g. noobmaster69"
                            value={username}
                            onChange={val => setUsername(val)}
                            data-testid="username"
                        />
                        <div>
                            <Input
                                label="Password"
                                type="password"
                                placeholder="For e.g. **************"
                                value={password}
                                onChange={val => setPassword(val)}
                                data-testid="password"
                            />
                            <p className="mt-2">
                                <a
                                    className="text-xs text-CD-blue cursor-pointer font-semibold"
                                    href="https://codedamn.com/contact"
                                    tabIndex={1}>
                                    Forgot Password?
                                </a>
                            </p>
                        </div>
                        <div>
                            <Button
                                label="Continue"
                                type="blue"
                                fullWidth
                                data-testid="login"
                                onClick={attemptUserLogin}
                                loading={busy}
                                disabled={busy}
                            />
                            <p className="text-center my-4">
                                <a
                                    className="text-xs cursor-pointer font-semibold"
                                    href="https://codedamn.com/login"
                                    tabIndex={1}>
                                    Regular Login
                                </a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <Head>
                <title>Creator Login | codedamn</title>
            </Head>
</div>

3
我注意到当加载时,main 标签消失了,还将 w-full md:min-w-full bg-CD-black-dark-accent rounded px-8 mx-4 sm:px-16 py-10 类更改为 h-screen w-full flex justify-center items-center mx-auto max-w-2xl text-white。请检查一下您的 React 应用程序中是否有任何函数更改了这些类。如果没有,请在 codesandbox 上共享以进行进一步调试。 - Surjith S M
请查看 https://github.com/vercel/next.js/issues/43878。 - AryanJ-NYC
4个回答

3

2

注意:这是一个临时解决方案,但可以完成工作

目前为止,我还没有找到任何合适的解决方案,但我和我的团队采用的一个折中的方法是在 tailwind.config.js 中更改一些东西,比如将自定义颜色上移或下移,然后保存。我认为它会重新运行 tailwind 编译器,然后类就开始起作用了。


或者你可以更改.scss文件中的某些内容,使类起作用!但这不是一个理想的解决方案! - rupesh45
谢谢,这解决了我的问题!我只是在tailwind.config.js内编辑了某些内容,而没有更改任何内容,然后出现故障的样式开始工作了。 - daniel souza

1

我无法找到这种奇怪行为背后的实际原因,但我使用一个基本的div作为具有特定类的div(s)的父级来解决了这个问题,这个问题在页面刷新时消失。

PS:我尝试了你的部署,它运行良好。我找不到你提到的问题。

对我来说,这样做是有效的。但Tailwind CSS仍然相当有缺陷。

<div>
    <div className={"flex flex-row w-screen p-2 items-center"}>
        {...content}
    </div>
</div>

1
你能否提供一个简单的示例来说明你的建议? - YannickIngenierie

0
我在使用router.push()方法进行页面导航时遇到了问题,但我通过以下方式解决了它: window.location.href='\'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接